javascript Access-Control-Allow-Methods 不允许 DELETE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28990565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:53:43  来源:igfitidea点击:

DELETE is not allowed by Access-Control-Allow-Methods

javascriptjquerygoogle-chromehttpcors

提问by M?rten Wikstr?m

I'm trying to send a cross-origin DELETErequest from Chrome using jQuery.

我正在尝试DELETE使用 jQuery 从 Chrome发送跨域请求。

However, that fail with the following error message being logged in the developer console:

但是,这会失败,并在开发人员控制台中记录以下错误消息:

XMLHttpRequest cannot load http://actual/url/here. Method DELETE is not allowed by Access-Control-Allow-Methods.

XMLHttpRequest 无法加载http://actual/url/here。Access-Control-Allow-Methods 不允许方法 DELETE。

The javascript code is running on localhost and looks like this:

javascript 代码在本地主机上运行,​​如下所示:

$.ajax({
    type: "DELETE",
    url: "http://actual/url/here",
    xhrFields: {
        withCredentials: true
    }
});

This results in a pre-flight request like this being sent:

这会导致发送这样的飞行前请求:

OPTIONS http://actual/url/here HTTP/1.1
Host: actual
Connection: keep-alive
Access-Control-Request-Method: DELETE
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Access-Control-Request-Headers: accept
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

And the response looks like this:

响应如下所示:

HTTP/1.1 200 OK
Cache-Control: must-revalidate, private
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Access-Control-Allow-Methods: DELETE GET HEAD POST PUT OPTIONS TRACE
Access-Control-Allow-Headers: accept
Access-Control-Max-Age: 900
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
Date: Wed, 11 Mar 2015 15:03:46 GMT

As far as I can tell this is just fine. The client checks whether DELETEis allowed by sending Access-Control-Request-Method: DELETEand the server says that it is allowed by responding with Access-Control-Allow-Methods: DELETE GET HEAD POST PUT OPTIONS TRACE.

据我所知,这很好。客户端DELETE通过发送检查是否允许,Access-Control-Request-Method: DELETE服务器通过响应来表示允许Access-Control-Allow-Methods: DELETE GET HEAD POST PUT OPTIONS TRACE

However, no DELETErequest is ever sent and the error message (above) is reported instead. Why?

但是,DELETE从未发送任何请求,而是报告错误消息(如上)。为什么?

回答by Quentin

The value of Access-Control-Allow-Methodsneeds to be a commaseparated list, not a space separated one.

的值Access-Control-Allow-Methods需要是逗号分隔的列表,而不是空格分隔的列表。

From MDN:

来自MDN

Access-Control-Allow-Methods: <method>[, <method>]*
Access-Control-Allow-Methods: <method>[, <method>]*