如何启用DELETE请求? [英] How to enable DELETE request?

查看:201
本文介绍了如何启用DELETE请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于CORS,我无法允许我的API服务器发出删除请求。

I'm not able to allow the DELETE request from my API server due to CORS.

// 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();
});

我收到控制台错误消息:

I get a console error saying:

XMLHttpRequest无法加载http:// localhost:8080 / api / users / 57f5036645c04700128d4ee0。飞行前响应中Access-Control-Allow-Methods不允许方法DELETE

如何启用 DELETE 请求,就像 GET PUT POST 请求?

How can I enable DELETE requests, just like GET, PUT, and POST requests?

推荐答案

我无需添加新软件包就解决了它,只是添加了这一行

I solved it without adding new packages, just added this line

res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");

请注意,我在一个字符串中用逗号分隔了允许的方法。
完整功能如下:

Notice that i have my allowed methods separated by commas inside one string. The complete function looks like this:

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();
});

这篇关于如何启用DELETE请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆