$getJSON .fail not firing
Asked Answered
G

1

0

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?

Gel answered 23/9, 2015 at 19:50 Comment(1)
jsonp requests don't always trigger errors due to how jsonp requests are made using script tags. The only workaround is to use jQuery 2.x or to send/process the jsonp requests yourself.X
X
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

X answered 23/9, 2015 at 19:58 Comment(1)
related: #32640729X

© 2022 - 2024 — McMap. All rights reserved.