在Node.js中允许CORS进行PUT [英] Allow CORS for PUT in Node.js
本文介绍了在Node.js中允许CORS进行PUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图对我的其余api端点进行 PUT
调用,并收到此错误:
<$ p在飞行前响应中,Access-Control-Allow-Methods不允许使用$ p>
方法PUT。
我使用以下解决方案启用了 CORS
: a href = https://enable-cors.org/server_expressjs.html rel = noreferrer>启用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,来源,X-Requested-With,Content-Type,Accept);
res.header('Access-Control-Allow-Methods','PUT,POST,GET ,DELETE,OPTIONS');
next();
});
I am trying to make a PUT
call to my rest api endpoint, and getting this error:
Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.
I enabled CORS
using this solution: enable-cors, it works for POST
.
How do I achieve the same for PUT
?
Thanks.
解决方案
add this:
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();
});
这篇关于在Node.js中允许CORS进行PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文