POST from jQuery to WCF Service works in Chrome, Firefox but not in IE (CORS issue)

有些话、适合烂在心里 提交于 2019-12-24 00:55:36

问题


I am trying to POST a search request from a jQuery/knockout web app. I followed the directions for implementing CORS in WCF and also added the following to the service:

    [OperationContract]
    [WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
    public void GetOptions()
    {
    }

The search request works fine in Chrome and Firefox. Chrome sends an OPTIONS request, which is handled successfully (200) and the access-control headers are in the response, and then it sends the POST, which returns search results. So the CORS preflight is working just fine.

But IE is a different story. It also sends and OPTIONS request which is handled successfully and the access-control headers are in the response, but then it just...stops. It doesn't send a POST request afterward and has a status of (aborted) in developer tools.

The console has an error of XMLHttpRequest: Network Error 0x80070005, Access is denied. but the status of the OPTIONS request is 200 and it never (from what I can tell) even attempts the POST. So I don't even know what was denied.

I'm pulling my hair out here. It would be obvious something was wrong in all three browsers, but why only IE? These are the headers I am adding:

requiredHeaders.Add("Access-Control-Allow-Origin", "*");
requiredHeaders.Add("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE,OPTIONS");
requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Accept");
requiredHeaders.Add("Access-Control-Max-Age", "1728000");

And this is the client side request:

$.ajax({
    url: serviceUrl + "ExemptServices.svc/json/search",
    method: "POST",
    contentType: "application/json",
    data: JSON.stringify(ko.mapping.toJS(self.Criteria)),
    success: function (response) {
        self.SearchResults(response.ResultData);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert("Error searching." + errorThrown);
    }
});

I should also mention that both the web and WCF servers are in the local intranet zone and everything is on the same internal network, including the client.

Here's the logs from fiddler. There's only the OPTIONS request, no attempt to perform a POST was made:

OPTIONS http://ecydevws2/ustservice/ExemptServices.svc/json/search HTTP/1.1
Accept: */*
Origin: http://ecyapdevtcp
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, accept
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: ecydevws2
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 0
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers: X-Requested-With,Content-Type,Accept
Access-Control-Max-Age: 1728000
Set-Cookie: ASP.NET_SessionId=z35robabmzflsyp0l1xl0qey; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Mon, 25 Jul 2016 17:49:43 GMT

来源:https://stackoverflow.com/questions/38530764/post-from-jquery-to-wcf-service-works-in-chrome-firefox-but-not-in-ie-cors-iss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!