简体   繁体   中英

JSONP error in JQuery 1.7

Note : I've checked other answers including this question re: a pre-jQuery 1.5 JSONP error issue that has now been resolved . I am using JQuery 1.7 which I believe does not suffer from this issue.

As of JQuery 1.5 proper error functions are possible with JSONP (see 'The jqXHR Object'). However the error function does not seem to be firing.

$.getJSON(url, function(response) { 
    console.log(response)   
}).error( function() { console.log('Error occurred.') } )   

Looking at Chrome Dev Tools, the JSONP request generates the following error:

GET https://twitter.com/status/user_timeline/somepretendusername.json?count=2&callback=jQuery171005595548846758902_1334772179012&_=1334772179132 400 (Bad Request)

However the .error() callback simply does not seem to run. What do I need to get proper error handling working with JSONP?

The error function only triggers if there is an error with the request. The request is being made, but Twitter returns a 400 if the username doesn't exist. I tried it using a fake username and got the same response you did, but then I used my user name and it worked like a champ.

[edit] I see that you are just asking how to handle errors with JSONP. For whatever reason, it seems that jQuery doesn't have good error handling for this. I did find a plugin that promises to have actual error handling for JSONP. Check it out here .

[edit2] Check it out, it works!

$(document).ready(function() {
    var url = 'https://twitter.com/status/user_timeline/thisisnotarealtwitterhandle.json?count=2&callback=?';
    var jqxhr = $.jsonp({
        url: url,
        success: function(response) {
            console.log(response)
        }
    }).fail(function() { console.log('error'); });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM