gpt4 book ai didi

express - 如何启用 DELETE 请求?

转载 作者:行者123 更新时间:2023-12-04 04:54:42 25 4
gpt4 key购买 nike

我不能允许 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com