Access-Control-Allow-Methods 不允许删除

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

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

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

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

javascript 代码在本地主机上是 运行,看起来像这样:

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

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

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

响应如下所示:

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

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

表示允许

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

Access-Control-Allow-Methods 的值必须是 逗号 分隔列表,而不是 space 分隔列表。

来自MDN

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

在我的案例中,以下配置有效。希望这会对某人有所帮助。 将此添加到 部分下的 Web API“web.config”。 我忘了我是从哪里得到这些信息的。

<modules>
  <remove name="WebDAVModule" />
</modules>
 <httpProtocol>
  <customHeaders>
    <add name="X-Frame-Options" value="DENY" />
    <add name="Access-Control-Allow-Methods" value="*" />
    <add name="Access-Control-Allow-Headers" value="*" />
  </customHeaders>
</httpProtocol>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <remove name="WebDAV" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>