在Node.js的和AngularJS CORS [英] CORS in Node.js and AngularJS
问题描述
我有一个CORS问题,我的应用程序。
I have a CORS problem with my app.
我的栈的Node.js与前preSS 4 并使用AngularJS的 Restangular
My stack is Node.js with Express 4 and AngularJS using Restangular
我已经尝试了一些东西(例如),但我不断收到:
I already have tried a few things (for example) but I keep getting:
XMLHttpRequest cannot load http://localhost:8080/api/users. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
在服务器端我有这样的标题:
In the server side I have this headers:
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, POST","PUT");
next();
});
在AngularJS部分我使用这样的:
In the AngularJS part I am using this:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
和我做与Restangular的帖子是这样的:
And I am doing the post with Restangular like this:
var baseUsers = Restangular.all('users');
....
....
baseUsers.post(newUser).then(function(user){
console.log(user);
});
还有一件事,在Node.js的服务器,我安慰了 req.method
和它说选项
,我真的不知道为什么。
One more thing, in the Node.js server I am consoling the req.method
and it says OPTIONS
, I really don't know why.
也许是愚蠢的东西,希望有人能帮助我。
Perhaps is something silly, hope someone can help me.
谢谢!
推荐答案
更改 -
res.header("Access-Control-Allow-Headers", "X-Requested-With");
到
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
<一个href=\"http://www.wilsolutions.com.br/content/fix-request-header-field-content-type-not-allowed-access-control-allow-headers\"相对=nofollow>源2
这篇关于在Node.js的和AngularJS CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!