我正在尝试对我的 rest api 端点进行 PUT
调用,并收到此错误:
Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.
我使用此解决方案启用了 CORS
:enable-cors ,它适用于POST
。
我如何为 PUT
实现相同的效果?
谢谢。
最佳答案
添加这个:
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
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', 'PUT, POST, GET, DELETE, OPTIONS');
next();
});
关于javascript - 在 Node.js 中允许 CORS 用于 PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42463499/