问题
I'm having problems with catching 403 from $.ajax
promise in jQuery v1.x.
The same code
$.ajax({
dataType: 'jsonp',
url: 'http://www.checkupdown.com/accounts/grpb/B1394343/',
type: 'GET'
}).then(function () {
console.log('success', arguments)
}, function () {
console.log('error', arguments)
});
rejects the promise as expected in jQuery v2.x but logs nothing in jQuery v1.x (the promise has readyState == 1
).
The examples use 2.1.3 and 1.11.3 jQuery versions respectively.
Why exactly does this happen? Is it solvable for jQuery v1?
回答1:
They perform differently because in 1.11.x, an error event handler is not attached to the script tag, instead all you have is an onload or onreadystatechange handler.
https://github.com/jquery/jquery/blob/1.11.3/src/ajax/script.js#L57
https://github.com/jquery/jquery/blob/2.1.3/src/ajax/script.js#L44
There is no workaround other than using a timeout or performing the jsonp request yourself.
回答2:
It's tricky because you're not making a real AJAX request. JSON-P is a hack, and will cause all sorts of problems for you.
If you really want to detect errors reliably, you need to use a real AJAX request.
来源:https://stackoverflow.com/questions/32640229/catching-403-from-ajax-promise-in-jquery-v1-x