简体   繁体   English

当休息api应用服务器(express)和Angulars js应用程序在不同端口上运行时,Cors会发出问题

[英]Cors issue when rest api application server(express) and Angulars js application running on different port

I have rest api application written in node.js & express is running on port 3000 and angularjs application running on port 9001 on same server . 我有一个用node.js编写的休息api应用程序&express运行在端口3000和在同一服务器上的端口9001上运行的angularjs应用程序。 On calling rst api from angularjs application it is giving cors issue . 在从angularjs应用程序调用rst api时,它会给出cors问题。

In rest api application , I have used "cors" module 在休息api应用程序中,我使用了“cors”模块

var cors = require('cors');

app.use(cors());

But it is giving me following error 但它给了我以下错误

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

Request header and response header is as follow 请求标头和响应标头如下

Response header 响应头

Content-Length 222
Content-Type application/json; charset=utf-8
Date Tue, 16 Dec 2014 08:40:05 GMT
X-Powered-By Express

view source 查看源

Request Header 请求标题

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

Thanks 谢谢

I solved it by adding adding line app.options('*', cors()); 我通过添加添加行app.options('*',cors())来解决它; just before all routes. 就在所有路线之前。

It was an issue of preflight request with http-request-header "OPTIONS" 这是一个带有http-request-header“OPTIONS”的预检请求问题

You can set the solution to be 您可以将解决方案设置为

app.configure('development', function(){
    app.set('origins', '*:*');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM