使用Chrome浏览到我的nodejs网站。我在Chrome开发人员工具中看到的Access-Control-Allow-Origin值不同于在nodejs
中定义的值。在我的nodejs中
res.setHeader('Access-Control-Allow-Origin', "siteA");
当我在“网络”->“响应标题”下查看Chrome开发者工具时,
Access-Control-Allow-Origin: siteB
尝试使用Edge-工作正常
我使用应用程序->清理存储清理站点数据-没帮助
我在Chrome中看到的错误代码是
Access to XMLHttpRequest at 'XXXXX' from origin 'siteA' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'siteB' that is not equal to the supplied origin.
答案 0 :(得分:0)
可以将res.setHeader('Access-Control-Allow-Origin', "siteA");
更改为res.setHeader('Access-Control-Allow-Origin', "*");
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods","POST","GET")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,authorization,Accept");
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
请将此代码复制并粘贴到服务器的index.js文件中。我认为这应该可行。