我不能允许 DELETE
由于 CORS,来自我的 API 服务器的请求。
服务器.js
// enable CORS
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET", "PUT", "POST", "DELETE", "OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
我收到一个控制台错误说:
XMLHttpRequest cannot load http://localhost:8080/api/users/57f5036645c04700128d4ee0. Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response
如何启用
DELETE
请求,就像 GET
, PUT
, 和 POST
要求?
最佳答案
我在没有添加新包的情况下解决了它,只是添加了这一行
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
请注意,我在一个字符串中用逗号分隔了允许的方法。
完整的函数如下所示:
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
关于express - 如何启用 DELETE 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39876110/