我的应用程序存在 CORS 问题。
我的堆栈是 Node.js 和 Express 4和 AngularJS 使用 Restangular
我已经尝试了一些东西 ( for example ) 但我不断得到:
XMLHttpRequest cannot load http://localhost:8080/api/users. Request header field Content-Type is not allowed by Access-Control-Allow-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 部分我使用这个:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
我正在用 Restangular 做这个帖子:
var baseUsers = Restangular.all('users');
....
....
baseUsers.post(newUser).then(function(user){
console.log(user);
});
还有一件事,在 Node.js 服务器中,我正在安慰 req.method
,它说 OPTIONS
,我真的不知道为什么。
也许是一些愚蠢的事情,希望有人能帮助我。
谢谢!
最佳答案
改变-
res.header("Access-Control-Allow-Headers", "X-Requested-With");
至
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
关于node.js - Node.js 和 AngularJS 中的 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25882106/