甚至在进行设置时,访问控制-允许-起源问题 [英] Access-Control-Allow-Origin issues even on setting it up
本文介绍了甚至在进行设置时,访问控制-允许-起源问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的nodejs/express包括:
My nodejs/express includes:
app.use(function handleAppError(err, req, res, next) {
const msg = 'ERROR: App error: ' + util.inspect(err);
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
console.log(msg);
if (res.headersSent) {
next(err);
} else {
res.status(500).send(msg);
}
});
…但是在拨打电话时,我仍然遇到Chrome错误:
…but on making a call, I still end up with an error in Chrome:
XMLHttpRequest无法加载http://:8080/management-api/v1/bots. 对预检请求的响应未通过访问控制检查:否 请求中存在"Access-Control-Allow-Origin"标头 资源.因此,不允许使用来源" http://localhost:8100 " 访问.
XMLHttpRequest cannot load http://:8080/management-api/v1/bots. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
推荐答案
您需要处理OPTIONS方法. 试试这个
You need to handle the OPTIONS method. Try this
app.all("/*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
if (req.method === "OPTIONS")
res.send(200);
else
next();
});
这篇关于甚至在进行设置时,访问控制-允许-起源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文