$getJSON .fail not firing

不打扰是莪最后的温柔 提交于 2019-12-13 07:39:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!