当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', '*:*'); }