2

我正在使用以下 jQuery blob 向启用 WCF Ajax 的 Web 服务发出请求

该站点本身托管在localhost:80,WCF 服务位于localhost:8080

$.ajax({
    type: "POST",
    url: String.format(Service, Method),
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(Data),
    timeout: 6000,
    dataType: "json",
    success: function (e) { OnSuccess(e); },
    error: function (e) { OnFailed(e); }
});

这在 IE 中运行良好,但是当我尝试在 Chrome 或 Firefox 中运行此代码时(即使在用户通过身份验证之后)我收到错误HTTP/1.1 401 Unauthorized。运行 fiddler 后,原因很清楚,因为 chrome 没有发送我为表单身份验证配置的 Cookie .ASPXFORMSAUTH。

具体来说,这就是 IE 请求的样子

POST /SchedulerService.svc/GetAllEventsByCurrentUser HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=utf-8
Referer: http://localhost/Calendar/Calendar.aspx
Accept-Language: en-AU
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Connection: Keep-Alive
Content-Length: 0
DNT: 1
Host: localhost:8080
Pragma: no-cache
Cookie: ASP.NET_SessionId=dmz5jv3oxa0llsph0thh1443; .ASPXFORMSAUTH=5EA7CB8124C5077933A639062999A89D35D440C6AD1A038C83A42D34694C20886506721D3CCD899BDA7B705CEF3B3024368AD6AE4523DEBDC5891E8DDD478206A3C2EF852345F70812F01D30F8F1041C2113EA2836CC5353FEAF81FC3EBF4DB6921D6DB270DE5C4102321DDD4D3923082B890995195990088749A1815B6A0BE5

与铬

POST /SchedulerService.svc/GetAllEventsByCurrentUser HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 0
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
Content-Type: application/json; charset=utf-8
Referer: http://localhost/Calendar/Calendar.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-AU,en;q=0.8,en-US;q=0.6,en-GB;q=0.4

任何人都可以就可能出现的问题提供任何指导吗?我意识到我可能需要提供更多信息,但不确定还有什么相关的。

编辑:在尝试了很多很多不同的想法之后,在我看来,我所有的问题都可能源于 IE、Chrome 和 Firefox 之间实施同源策略的巨大差异。当我有更多时会更新......

4

1 回答 1

0

由于您的 asp.net 和 wcf 应用程序似乎托管在不同的端口(80 和 8080)上,您可以尝试beforeSend发送凭据:

$.ajax({
    type: "POST",
    url: String.format(Service, Method),
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(Data),
    timeout: 6000,
    dataType: "json",
    success: function (e) { OnSuccess(e); },
    error: function (e) { OnFailed(e); },
    beforeSend: function(xhr){
       xhr.withCredentials = true;
    }
});

https://stackoverflow.com/a/2054370/1236044

于 2013-06-04T07:49:55.410 回答