我正在编写一个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上可用
答案 0 :(得分:2)
使用setHeader
,您只能写一个标题,覆盖之前的标题。您必须使用允许您编写多个标头的res.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();
});