我正在尝试使用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
据我所知,这很好。客户端通过发送DELETE
来检查是否允许Access-Control-Request-Method: DELETE
,并且服务器通过Access-Control-Allow-Methods: DELETE GET HEAD POST PUT OPTIONS TRACE
回复来表示允许{。}}。
但是,始终没有发送DELETE
请求,而是报告错误消息(上面)。的为什么吗
答案 0 :(得分:23)
Access-Control-Allow-Methods
的值必须是逗号分隔列表,而不是空格分隔列表。
来自MDN:
Access-Control-Allow-Methods: <method>[, <method>]*
答案 1 :(得分:0)
在我的情况下,以下配置有效。希望这会对某人有所帮助。
将此添加到
<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>