我正在使用以下blob的jQuery向启用WCF Ajax的webservice发出请求
网站本身托管在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
VS CHROME
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之间相同原始政策实施的巨大差异。当我有更多......
时会更新答案 0 :(得分: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;
}
});