当restAPI应用程序服务器(express)和Angulars js应用程序运行在不同的端口时,Cors问题
我有restapi应用程序写在node.js&express是运行在端口3000和angularjs应用程序运行在同一台服务器上的端口9001上。 从angularjs应用程序调用rst api是给cors问题。
在其他api应用程序中,我使用了“cors”模块
var cors = require('cors'); app.use(cors());
但它给了我下面的错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/user/login. This can be fixed by moving the resource to the same domain or enabling CORS
请求头和响应头如下
响应标题
Content-Length 222 Content-Type application/json; charset=utf-8 Date Tue, 16 Dec 2014 08:40:05 GMT X-Powered-By Express
查看源代码
请求标题
Accept application/json, text/plain, / Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Authorization null Content-Length 47 Content-Type application/json;charset=utf-8 Host localhost:3000 Origin http://localhost:9001 Referer http://localhost:9001/ User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
谢谢
我通过添加行app.options('*',cors()); 在所有路线之前。
这是一个使用http-request-header“OPTIONS”进行预检请求的问题
你可以设置解决scheme
app.configure('development', function(){ app.set('origins', '*:*'); }
- 使用发送事件以编程方式创buildDropzone发送附加数据
- 运行“业力:单位”(业)任务警告:无法加载“HTML”,它没有注册! 也许你错过了一些插件?
- 预期的响应包含一个对象,但有一个数组
- AngularJS和NodeJS中的视图模板引擎之间的概念区别是什么?
- 我怎样才能返回对应于node.js中的URL请求的XML文件?
- 如何将angular度uiselect结果绑定到ui-grid / ng-grid
- Node.js + AngularJS + Socket.io:推送的数据在控制器中不可用
- Babel + Typescript + NPM模块+ Webpack
- 试图链接到angularjs应用程序中的特定页面