拦截所有ajax调用? [英] Intercept all ajax calls?

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

问题描述

我正在尝试拦截所有 AJAX 调用,以检查该 AJAX 响应是否包含我从 PHP 脚本作为 JSON 发送的特定错误代码(代码: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) {
});

我可以在 jQuery 或原始 JavaScript 中执行此操作.

I can do this in either jQuery or raw JavaScript.

推荐答案

来自 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 元素开始的),而是定义了一个处理范围被默认(即 this 将指向那个/那些元素).

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天全站免登陆