gpt4 book ai didi

javascript - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 Origin 'https ://myappurl. com 因此不允许访问

转载 作者:行者123 更新时间:2023-12-01 02:05:02 25 4
gpt4 key购买 nike

我正在编写一个 vue 应用程序并使用 Node.js Api。我已经将 cors 包含在后端中,其他 post 方法效果很好。但在这条路由中有一个 Nginx 502 Bad Gateway。

chrome控制台报错为

Failed to load resource: the server responded with a status of 502 (Bad Gateway)

Failed to load : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access. The response had HTTP status code 502.

app.js CORS

app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization");
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.setHeader("Access-Control-Allow-Credentials", "false")

next();
});

我的AddPatient组件和我的Axios可在 Pastebin 上找到

最佳答案

使用setHeader,您只编写一个 header ,覆盖前一个 header 。您必须使用 res.header() 来允许您编写多个 header 。你可以这样做:

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header("Access-Control-Allow-Credentials", "false")

next();
});

关于javascript - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 Origin 'https ://myappurl. com 因此不允许访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com