简体   繁体   English

JQuery 1.7中的JSONP错误

[英]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 . 注意 :我已经检查了其他答案,包括以下问题:re-jQuery 1.5之前的JSONP错误问题,现已解决 I am using JQuery 1.7 which I believe does not suffer from this issue. 我正在使用JQuery 1.7,我相信它不会遇到此问题。

As of JQuery 1.5 proper error functions are possible with JSONP (see 'The jqXHR Object'). JQuery 1.5开始,JSONP可以使用适当的错误功能 (请参见“ jqXHR对象”)。 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: 查看Chrome开发者工具,JSONP请求生成以下错误:

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. 但是,.error()回调似乎根本没有运行。 What do I need to get proper error handling working with JSONP? 我需要什么才能使用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. 正在发出请求,但如果用户名不存在,Twitter将返回400。 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. [edit]我看到您只是在问如何使用JSONP处理错误。 For whatever reason, it seems that jQuery doesn't have good error handling for this. 无论出于什么原因,看来jQuery对此都没有很好的错误处理能力。 I did find a plugin that promises to have actual error handling for JSONP. 我确实找到了一个承诺对JSONP进行实际错误处理的插件。 Check it out here . 在这里查看

[edit2] Check it out, it works! [edit2]签出来,行得通!

$(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'); });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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