【问题标题】:DELETE is not allowed by Access-Control-Allow-Methods访问控制允许方法不允许删除
【发布时间】:2015-05-13 11:24:49
【问题描述】:

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

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

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

javascript 代码在 localhost 上运行,如下所示:

$.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 请求,而是报告了错误消息(如上)。 为什么?

【问题讨论】:

标签: javascript jquery google-chrome http cors


【解决方案1】:

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

来自MDN

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

【讨论】:

【解决方案2】:

在我的情况下,配置工作正常。希望这会对某人有所帮助。 将此添加到 部分下的 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>

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2022-06-25
    • 2019-12-15
    • 2017-10-15
    相关资源
    最近更新 更多