问题
Can't alert $getJSON .fail:
this.flickrPics = ko.observableArray();
ko.computed(function() {
$.getJSON(
'https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?',
{
tags : data.name,
format : 'json'
})
.done(function(response) {
for (var i = 0; i < 10; i++) {
self.flickrPics.push(response.items[i].media.m);
}
})
.fail(function(error) {
alert(error + 'error');
// $('.pics-box h2').text("sorry, pictures cant be loaded at the moment");
});
}, this);
Everything works perfect except for .fail. If i mess up with url, nothing happens, only get console errors of failed ajax calls. What am I doing wrong?
回答1:
To preserve compatibility with older browsers, jQuery 1.11.x uses .onreadystatechange to track when the jsonp request has returned. Because of this, it is not possible to track errors other than timeout errors when sending script or jsonp requests.
To get around this, you should be using jQuery 2.x in modern browsers and only including 1.11.x in older browsers so that the majority of your users will get the better error handling. The only way to fix it for the other users would be to not use jQuery to send this request and to instead create the script tag yourself and find a way of tracking success/error that works in all of your supported browsers. Or you could proxy this request with your server and do a normal XMLHTTP request.
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
来源:https://stackoverflow.com/questions/32748315/getjson-fail-not-firing