拦截所有的Ajax调用? [英] intercept all ajax calls?

查看:156
本文介绍了拦截所有的Ajax调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拦截所有这些都是为了AJAX调用,以检查是否是Ajax响应包含了我送的JSON从我的PHP脚本的特定错误code(codeS:ACCESS_DENIED,SYSTEM_ERROR,NOT_FOUND)。

I'm trying to intercept all AJAX calls in order to check if that AJAX response contains specific error code that I send as JSON from my PHP script (codes: ACCESS_DENIED, SYSTEM_ERROR, NOT_FOUND).

我知道我们可以做这样的事情:

I know one can do something like this:

$('.log').ajaxSuccess(function(e, xhr, settings) {
});

但是 - 这是否工作,只有当的ajaxSuccess事件冒泡到的.log DIV?我对么?我能达到我想要通过结合的ajaxSuccess事件文件?

But - does this work only if "ajaxSuccess" event bubble up to .log div? Am I correct? Can I achieve what I want by binding "ajaxSuccess" event to document?

$(document).ajaxSuccess(function(e, xhr, settings) {
});

P中。 S.任何见解,欢迎,jQuery的或生的JavaScript。谢谢!

P. S. Any insights are welcome, jQuery or raw javascript. Thanks!

推荐答案

HTTP://api.jquery。 COM /的ajaxSuccess /

当一个Ajax请求成功完成,jQuery的触发事件的ajaxSuccess。已注册的.ajaxSuccess()方法中的任何和所有的处理程序在这个时候被执行。

Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the .ajaxSuccess() method are executed at this time.

所以选择不定义你在哪里恶补的情况下(因为,说实话,AJAX事件由于其性质并不会从DOM元素开始)的位置,而是定义了一个范围,它的处理会被拖欠(即将poitn到/这些元素(S))。

So the selector doesn't define the position where you are "catching" the event (because, honestly, ajax event by its nature doesn't start from a DOM element), but rather defines a scope to which the handling will be defaulted (i.e. this will poitn to that/those element(s)).

在总结 - 它应该是正是你想要的。

In summary - it should be exactly what you wish for

这篇关于拦截所有的Ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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