相关疑难解决方法(0)

Node.js和AngularJS中的CORS

我的应用程序存在CORS问题.

我的堆栈是带有Express 4的 Node.js 和使用Restangular的 AngularJS

我已经尝试过一些东西(例如),但我一直在努力:

XMLHttpRequest cannot load http://localhost:8080/api/users. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Run Code Online (Sandbox Code Playgroud)

在服务器端,我有这个标题:

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();
});
Run Code Online (Sandbox Code Playgroud)

在AngularJS部分我使用这个:

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
Run Code Online (Sandbox Code Playgroud)

而我正在使用Restangular这样做:

var baseUsers = Restangular.all('users');
....
....
baseUsers.post(newUser).then(function(user){
console.log(user);
});
Run Code Online (Sandbox Code Playgroud)

还有一件事,在Node.js服务器中,我正在安慰req.methodOPTIONS,它说,我真的不知道为什么.

也许是傻事,希望有人可以帮助我.

谢谢!

node.js express angularjs restangular

5
推荐指数
1
解决办法
4420
查看次数

标签 统计

angularjs ×1

express ×1

node.js ×1

restangular ×1