ajaxStop()不会触发 [英] ajaxStop() doesn't fire

查看:126
本文介绍了ajaxStop()不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是无效的代码:

  $(document).ajaxStop(function(){
$(this).unbind(ajaxStop); //当其他调用完成时阻止再次运行

//显示所有内容
display();
});

这是我的Ajax功能:

  function getAjax(url,callback){
jQuery.ajaxPrefilter(function(options){
options.global = true;
});

$ .ajax({
url:url,
类型:GET,
dataType:jsonp,
成功:回调
});
}

为什么 ajaxStop()从不开火?

解决方案

你会注意到我正在制作JSONP请求。我花了很长时间才找到这个,但是这个问题的答案可以在这里



从机票:


JSONP请求无法保证完成(因为错误不是
捕获的)。在这种情况下,jQuery 1.5强制全局选项为false所以
内部ajax请求计数器保证在某一点或另一点恢复到
零。



如果您希望所有请求都能触发事件,无论是什么(并且存在相同的1.4.4不一致的风险),您可以使用以下预过滤器:




  jQuery.ajaxPrefilter(function(options){
options.global = true;
});




案例: http://jsfiddle.net/X4JTx/



This is the code that wasn't working:

$(document).ajaxStop(function() {
    $(this).unbind("ajaxStop"); //prevent running again when other calls finish

    // Display everything
    display();
});

And here's my Ajax function:

function getAjax(url, callback) {
    jQuery.ajaxPrefilter(function( options ) {
        options.global = true;
    });

    $.ajax({
        url: url,
        type: "GET",
        dataType: "jsonp",
        success: callback
    });
}

Why does ajaxStop() never fire?

解决方案

You'll notice I was making JSONP requests. It took me forever to find this, but the answer to this issue can be found here.

From the ticket:

JSONP requests are not guaranteed to complete (because errors are not caught). jQuery 1.5 forces the global option to false in that case so that the internal ajax request counter is guaranteed to get back to zero at one point or another.

If you want all requests to fire the events, no matter what (and at the risk of the same inconsistencies 1.4.4 exhibited), you can use the following prefilter:

jQuery.ajaxPrefilter(function( options ) {
    options.global = true;
});

Case in point: http://jsfiddle.net/X4JTx/

这篇关于ajaxStop()不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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