简体   繁体   English

在执行任何绑定的回调之前修改ajax响应

[英]Modify ajax response before any bound callbacks get executed

ISSUE 问题

I needed a way to modify all ajax responses in an app before all bound callbacks are triggered. 我需要一种在触发所有绑定的回调之前修改应用程序中所有ajax响应的方法。

SOLUTION

Standard ajax events, both global and local do not provide a good way to modify the response before app bound callback are triggered. 标准ajax事件,无论是全局的还是本地的,都无法在触发应用绑定回调之前提供一种修改响应的好方法。

The order by which ajax events get triggered is the one indicated in the jquery's ajax event docs ie 触发ajax事件的顺序是jquery的ajax事件文档中指出的顺序,即

  • ajaxStart (Global Event) ajaxStart(全球事件)
  • beforeSend (Local Event) beforeSend(本地事件)
  • ajaxSend (Global Event) ajaxSend(全球事件)
  • success (Local Event) 成功(本地活动)
  • ajaxSuccess (Global Event) ajaxSuccess(全球事件)
  • error (Local Event) 错误(本地事件)
  • ajaxError (Global Event) ajaxError(全局事件)
  • complete (Local Event) 完成(本地活动)
  • ajaxComplete (Global Event) ajaxComplete(全局事件)
  • ajaxStop (Global Event) ajaxStop(全球事件)

Notice that there is no hook executed after the response gets back from the server but before success/erorr callback are run eg a kind of ajaxReturn after ajaxSend but before success|error 注意,从服务器返回响应之后,但在运行成功/ erorr回调之前,没有执行任何钩子,例如,在ajaxSend之后但在success|error之前运行的一种ajaxReturn

The way i hacked this was to use $.ajaxPrefilter which is executed before ajaxStart and to wrap the already bound success/error callbacks in another function which will allow me to modify the options object and thus the returned data. 我的破解方法是使用$ .ajaxPrefilterajaxStart之前执行, ajaxStart已经绑定的成功/错误回调包装在另一个函数中,这将允许我修改options对象,从而修改返回的数据。

Below is the utility function that does the wrapping and an example: 以下是执行包装的实用程序功能和示例:

var alterResponse = function (opts) {
    var callback = function (options, originalOptions, jqXHR) {
        if (options.url.match(opts.urlMatch)) {
            // Cache original callback.
            var originalSuccess = originalOptions.success || options.success;
            originalOptions.success = options.success = function () {
                // Call wrapper that will modify the response object.
                opts.successWrapper.call(null, options, originalOptions, jqXHR, originalSuccess);
            };
        }
    };
    if (opts.dataTypes) {
        $.ajaxPrefilter(opts.dataTypes, callback)
    }
    else {
        $.ajaxPrefilter(callback);
    }
};

alterResponse({
    urlMatch: /myurl/g, // Filter urls you what to tamper with.
    successWrapper: function (options, originalOptions, jqXHR, originalFn) {
        options.data.customVar = 'customValue';
        originalFn(options, originalOptions, jqXHR);
    }
});

I have to mention i'm using this for testing purposes so i'm not concerned about performance, memory and overhead. 我不得不提到我将其用于测试目的,因此我不关心性能,内存和开销。

If you'd like to modify the data coming back from the server before it reaches the event handler, use dataFilter: 如果要在服务器到达事件处理程序之前修改从服务器返回的数据,请使用dataFilter:

    jquery.ajaxSetup({
        dataFilter: function (data, type) {
            //modify your data here

            return data;
        }
    });

You can use any of the global ajax events 您可以使用任何全局ajax事件

$(document).ajaxSuccess(function() {
   alert("An individual AJAX call has completed successfully");
 });
 //or...
$(document).ajaxComplete(function() {
  alert("ALL current AJAX calls have completed");
});

//or.. 
$(function() {
$.ajaxSetup({
    complete: function(xhr, textStatus) {
        // will be raised whenever an AJAX request completes (success or failure)
    },
    success: function(result) {
        // will be raised whenever an AJAX request succeeds
    },
    etc ... you could use any available option
});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM