下面的代码在收到“401 - Unauthorized”错误时抛出错误而不转到.fail或.always。有没有办法触发失败并检索来自服务器的错误[myErrorMessage]?
的JavaScript
$(document).ready(function() {
var jqXHR = $.ajax({
url: "http://[myDomain]/[myPage]",
dataType: "jsonp",
jsonpCallback: "myFunction"
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });
function myFunction(data) {
alert("callback");
}
});
请求结果
GET http://[myDomain]/[myPage]?callback=myJsFunction&_=1361463044315 HTTP/1.1
Host: [myDomain]
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
Referer: http://[myTotallyDifferentDomain]/Default.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
回应结果
HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 21 Feb 2013 16:10:42 GMT
Content-Length: 67
{"Message":"[myErrorMessage]"}
Chrome结果
GET http://[myDomain]/[myPage]?callback=myJsFunction&_=1361463044315 401 (Unauthorized) jquery.min.js:5
答案 0 :(得分:0)
自jQuery 1.9起,JSONP请求不会在出错时转到失败处理程序,这是记录的行为。
据我所知,没有办法绕过它。 (当然,除了使用代理)