我已阅读以上所有链接,但仍然没有解决我的问题。 Node 代码
app.all('*', (req, res, next) => {
let allowedOrigins = ['http://localhost:8080', 'http://127.0.0.1:8080'];
let origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
console.log(origin);
res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8080');
}
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Methods', "GET,POST,PUT,OPTIONS,DELETE");
next();
});
AngularJS
app.config(function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.useXDomain = true;
});
我实际上点击了该 url,它也返回了 200 OK 响应,但它仍然失败并显示这样的消息。我已经尝试了很多博客和答案,但似乎没有一个有帮助,并且每个人都重申了我已经尝试过的同样的事情。 失败的消息
XMLHttpRequest cannot load http://localhost:5040/save. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access.
最佳答案
我在这里只能预测一个问题。
Your app.use(), must be above all the requests.
我已经因为这种问题而焦头烂额了,所以我知道。如果有帮助请告诉我。如果没有,我们将重新开始。
关于angularjs - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 NodeJS 和 AngularJS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784021/