我有一个奇怪的问题。 在浏览器上放置请求时出现cors错误:
access to fetch at 'http://localhost:3015/pathtest/api/v1/results/cc7637578fad1a6fcfb4249fbf000a13/load' from origin 'https://localhost:9444' has been blocked by CORS policy: Method PUT is not allowed by access-Control-Allow-Methods in preflight response.
这是我的在后端(nodejs)中启用cors的代码。其他所有api请求均已通过以下代码修复,并且仅将put仍返回cors问题:
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");
// res.header('access-Control-Allow-Methods','PATCH,PUT,POST,GET,DELETE,OPTIONS');
// // allow preflight
// if (req.method === 'OPTIONS') {
// res.send(200);
// } else {
// next();
// }
res.header('access-control-allow-origin','*');
res.header('access-Control-Allow-Methods','GET,PATCH,OPTIONS');
res.header('access-Control-Allow-Headers','Content-Type,Authorization,Content-Length,X-Requested-With');
// allow preflight
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
});
我想念什么吗?
**更新