NodeJS , AngularJS not getting response when on different domain, getting success but without response data

时间:2016-02-03 03:17:29

标签: angularjs node.js

I developed an application based on MEAN stack and it works fine when UI and api on same domain-port. however , when I segregate UI and api and let say deploying on different domain or even local with different port ,all api call successful but no response data. FYI I have Access-Control-Allow-Origin * and without that also behaviour is same.

1 个答案:

答案 0 :(得分:0)

如果在Cross origin浏览器的情况下,首先发送选项请求。如果响应OPTION请求,如果发现 access-control-allow-origin ,则会发送实际请求,否则会产生CROS错误。

您可以在nodeJS代码的app.js中添加这些行。 这将解决您的目的。

app.use(function(req, res, next) {
  res.setHeader('access-control-allow-headers', 'Origin, X-Requested-With, Content-Type, Accept');
  res.setHeader('access-control-allow-methods', 'POST,HEAD,GET,PUT,DELETE,OPTIONS');
  res.setHeader('access-control-allow-origin', '*');
  next();
});

app.options('*', function(req, res, next) {
  res.send();
});