我在发布CORS请求时遇到了特定于chrome的问题。请求始终在OPTIONS的飞行前请求中失败。在IE 11中,根本不发送飞行前请求。
我的请求是使用jQuery发送的 -
$.ajax({
url: "http://localhost/Family/MessengerService.svc",
crossdomain: true,
headers: {
"SOAPAction": '"http://home/FamilyConnect/MessengerService/IMessengerService/Conversation"',
},
type: "POST",
dataType: "xml",
data: soapRequest,
processData: true,
complete: function (xmlHttpResponse, status) {
if (status == 'success') {
//Code to parse the return value.
}
},
contentType: "text/xml; charset=\"utf-8\""
});
我的请求标题是这个 -
OPTIONS /Family/MessengerService.svc HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Access-Control-Request-Method: POST
Origin: http://localhost:51481
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Access-Control-Request-Headers: accept, soapaction, content-type
Accept: */*
Referer: http://localhost:51481/Ping.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,fr;q=0.6
我收到的回复标题是 -
HTTP/1.1 400 Bad Request
Cache-Control: private
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: http://localhost:51481
Access-Control-Allow-Headers: accept, soapaction, content-type
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Date: Fri, 06 Feb 2015 21:50:24 GMT
Content-Length: 0
服务配置文件具有以下配置 -
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:51481"/>
<add name="Access-Control-Allow-Headers" value="accept, soapaction, content-type"/>
<add name="Access-Control-Allow-Methods" value="POST, GET, PUT, DELETE, OPTIONS"/>
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
我无法弄清楚为什么飞行前请求失败了? 请求失败,HTTP错误代码400 - 错误请求。从请求和响应标头,我猜我从jQuery发送的请求很好。 我应该查看我的IIS 8.5配置吗?如果是这样我该怎么办? 搜索SO和Google会产生更多结果,其中服务是RESTful,数据交换模式是JSON或JSONP。现在,是否应该表示带有SOAP协议的Xml不适合CORS请求?