我有一个奇怪的问题。在浏览器上放置请求时出现 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,PUT,POST,DELETE,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();
}
});
我错过了什么吗?
**更新