jQuery的阿贾克斯(JSONP)忽略超时而不会触发错误事件 [英] jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event

查看:163
本文介绍了jQuery的阿贾克斯(JSONP)忽略超时而不会触发错误事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要添加一些基本的错误处理,我想重写一张code,它使用jQuery的$ .getJSON拉在一些照片来自Flickr。在这样做的原因是,$ .getJSON不提供错误处理或与超时工作

To add some basic error handling, I wanted to rewrite a piece of code that used jQuery's $.getJSON to pull in some photo's from Flickr. The reason for doing this is that $.getJSON doesn't provide error handling or work with timeouts.

由于$ .getJSON仅约$。阿贾克斯我决定重写的东西和惊喜惊喜的包装,它完美的作品。

Since $.getJSON is just a wrapper around $.ajax I decided to rewrite the thing and surprise surprise, it works flawlessly.

现在,有趣的开始,虽然。当我故意造成404(通过改变URL)或导致网络超时(以不被挂接到interwebs),错误事件不火,在所有。我茫然,什么我做错了。帮助是非常AP preciated。

Now the fun starts though. When I deliberately cause a 404 (by changing the URL) or cause the network to timeout (by not being hooked up to the interwebs), the error event doesn't fire, at all. I'm at a loss as to what I'm doing wrong. Help is much appreciated.

这里的code:

$(document).ready(function(){

    // var jsonFeed = "http://api.flickr.com/services/feeds/photos_public.gne"; // correct URL
    var jsonFeed = "http://api.flickr.com/services/feeds/photos_public.gne_______"; // this should throw a 404

    $.ajax({
        url: jsonFeed,
        data: { "lang" : "en-us",
                "format" : "json",
                "tags" : "sunset"
        },
        dataType: "jsonp",
        jsonp: "jsoncallback",
        timeout: 5000,
        success: function(data, status){
            $.each(data.items, function(i,item){
                $("<img>").attr("src", (item.media.m).replace("_m.","_s."))
                          .attr("alt", item.title)
                          .appendTo("ul#flickr")
                          .wrap("<li><a href=\"" + item.link + "\"></a></li>");
                if (i == 9) return false;
            });
        },
        error: function(XHR, textStatus, errorThrown){
            alert("ERREUR: " + textStatus);
            alert("ERREUR: " + errorThrown);
        }
    });

});

我想补充一点,这个问题被问何时jQuery的是在1.4.2版本

I'd like to add that this question was asked when jQuery was at version 1.4.2

推荐答案

jQuery的1.5和更高版本有错误处理JSONP请求的更好的支持。但是,你需要使用 $。阿贾克斯方法,而不是 $。的getJSON 。对我来说,这部作品:

jQuery 1.5 and higher have better support for error handling with JSONP requests. However, you need to use the $.ajax method instead of $.getJSON. For me, this works:

var req = $.ajax({
    url : url,
    dataType : "jsonp",
    timeout : 10000
});

req.success(function() {
    console.log('Yes! Success!');
});

req.error(function() {
    console.log('Oh noes!');
});

超时,似乎这样的伎俩,并调用错误处理程序,当有10秒后没有成功的请求。

The timeout seems to do the trick and call the error handler, when there is no successful request after 10 seconds.

我做了一点博文在这个问题上为好。

I did a little blogpost on this subject as well.

这篇关于jQuery的阿贾克斯(JSONP)忽略超时而不会触发错误事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆