当REST API的应用服务器(如preSS)和Angulars JS应用程序的不同端口上运行CORS问题 [英] Cors issue when rest api application server(express) and Angulars js application running on different port

查看:108
本文介绍了当REST API的应用服务器(如preSS)和Angulars JS应用程序的不同端口上运行CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在node.js中与放大器REST API的应用程序;前preSS在端口3000上运行,并在同一台服务器上的端口9001上运行的应用程序angularjs。从angularjs应用程序中调用API首先是给CORS问题。

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 . On calling rst api from angularjs application it is giving cors issue .

在REST API的应用程序,我曾用CORS模块

In rest api application , I have used "cors" module

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

响应头

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()); 只是所有路由之前。

I solved it by adding adding line app.options('*', cors()); just before all routes.

这是preflight请求的问题,与HTTP请求头选项

It was an issue of preflight request with http-request-header "OPTIONS"

这篇关于当REST API的应用服务器(如preSS)和Angulars JS应用程序的不同端口上运行CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆