我有一个在需要使用JQuery $ .ajax方法(目前使用jquery 1.7.2)的网页上运行的脚本向不同域上的服务端点提交几个GET请求.我在IE(9,10,11)中使用了ajax调用,但它在Firefox和Chrome中失败了401 Unauthorized响应.Chrome中的部分其他错误消息是"访问此资源需要完全身份验证".
我的ajax调用设置如下(对于这些失败的请求,dataType为"json",async为true):
$.ajax({
url: url,
type: "GET",
async: isAsync,
dataType: dataType,
username: user,
password: pswd,
success: function (response, status) {
// success code here
},
failure: function (response, status) {
// failure code here
},
complete: function (xhr, status) {
// on complete code here
}
});
Run Code Online (Sandbox Code Playgroud)
我传递了访问该服务所需的用户名和密码,这在IE中有效.我理解JQuery ajax函数将正确处理身份验证,因此如果响应返回指示需要授权,它将使用提供的凭据来正确地生成该请求.我在这里错过了什么吗?我是否需要手动添加Authorization标头才能生效?
更新:以下是Chrome和IE通过F12调试工具报告的请求,响应和Cookie信息(某些信息已替换为[...已删除...])
铬(42.0.2311.90米)
响应标题
access-control-allow-credentials:true access-control-allow-origin:[... removed ...] access-control-expose-headers:cache-control:private,max-age = 0,must-revalidate connection :keep-alive content-encoding:gzip content-length:296 content-type:text/html; charset = ISO-8859-1 date:Tue,21 Apr 2015 20:55:12 GMT expires:Tue,2015年4月21日20 :55:12 GMT p3p:CP …