jQuery全局(和本地)ajax事件未触发 [英] jQuery global (and local) ajax events are not firing

查看:61
本文介绍了jQuery全局(和本地)ajax事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查: http://jsfiddle.net/TWiStErRob/s2jSA/在我尝试设置的位置我想到的所有可能的变化:

Please check: http://jsfiddle.net/TWiStErRob/s2jSA/ where I try set up all possible variations I could think of for:

success, error, complete
ajaxSetup
ajaxSuccess, ajaxError, ajaxComplete
done, fail, always

如我所见:

  1. JSONP缺少许多事件
    我希望得到与JSON相同的输出
  2. 全局AJAX事件不像Deferred的回调那样起作用,即注册顺序很重要,而仅适用于相同类型的事件.
    没什么大不了的,我可以忍受.
  3. 成功/错误后,
  4. complete正在运行
    很高兴知道.
  1. a lot of events are missing for JSONP
    I would expect the same output as for JSON
  2. Global AJAX events don't work as the Deferred's callbacks, i.e. registration order matters, but only with the same type of events.
    Not a biggie, I can live with it.
  3. complete is running after success/error
    Good to know.

对于JSONP来说,事件几乎没有用,有人可以解释原因并提供解决方法吗?

It seems that for JSONP the events are almost useless, can someone please explain why and give a workaround?

推荐答案

原因

来自 http://api.jquery.com/category/ajax/global-ajax-event-handlers/


注意:全局事件的值永远不会触发跨域脚本或JSONP请求.

来自 http://bugs.jquery.com/ticket/8338


不能保证JSONP请求完成(因为未捕获错误).在这种情况下,jQuery 1.5强制将global选项设置为false,以确保内部ajax请求计数器在某一点或另一点恢复为零.

From http://bugs.jquery.com/ticket/8338


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.

如果您希望所有请求均触发事件,则无论采取何种措施(并冒出现相同的不一致1.4.4的风险),都可以使用以下预过滤器:

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;
});

解决方法

上面的代码确实可以使它工作:

Workaround

The above code indeed makes it work:

$.ajaxPrefilter(function global_ajaxPrefilter(options, originalOptions, jqXHR) {
    options.global = true;
});
$(document).ajaxSuccess(function global_ajaxSuccess(event, XMLHttpRequest, ajaxOptions) {
    if(config.logResponses) {
        console.log(XMLHttpRequest.responseText);
    }
});
$(document).ajaxError(function global_ajaxError(event, jqXHR, ajaxSettings, thrownError) {
    console.error("error: " + jqXHR.status + " " + thrownError);
});

但是出于我的目的,以下方法效果更好:

However for my purposes the following approach works better:

$.ajaxPrefilter(/*dataTypes, */ function global_ajaxPrefilter(options, originalOptions, jqXHR) {
    if(config.logResponses) {
        jqXHR.done(function global_ajaxSuccess(data, textStatus, jqXHR) {
            console.groupCollapsed(options.url + (options.data ? '&' + $.param(options.data) : ''));
            console.log("Options: " + JSON.stringify(options));
            console.log("Data: " + JSON.stringify(data));
            console.groupEnd();
        });
    }
    jqXHR.fail(function global_ajaxError(jqXHR, textStatus, errorThrown) {
        console.error(textStatus + ": " + errorThrown));
    });
});

注意global_ajaxSuccessglobal_ajaxError的不同参数列表:

Notice the different argument list of global_ajaxSuccess and global_ajaxError:

  • options:在两个解决方案中均可用(包含URL)
  • data:在全局处理程序中可用作文本,在done
  • 中作为对象
  • 界面对done/error
  • 非常熟悉
  • options: available in both solutions (contains URL)
  • data: is available as text in global handler, and object in done
  • interface is much familiar with done/error

这篇关于jQuery全局(和本地)ajax事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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