覆盖阿贾克斯成功事件 [英] Override Ajax Success event

查看:119
本文介绍了覆盖阿贾克斯成功事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖jQuery的 AJAX 函数来处理一个默认的行动是成功的事件,但也执行,我使用了<$ C $回调函数C>选项参数。 的目的是什么有标签返回的响应,我总是想要剥离用于其他地方的响应。

该方案是:

  • 在阿贾克斯提交
  • 在阿贾克斯成功
  • - 默认成功操作
  • - 呼叫阿贾克斯成功回调

谁能帮助? 我试图延长

  • jQuery.ajax
  • jQuery.ajaxSuccess
  • jQuery.ajax.done

在code我拥有的是:

  VAR _ajaxSuccess = jQuery.fn.ajaxSuccess;
$ .fn.extend({
    的ajaxSuccess:功能(一)
    {
        _ajaxSuccess.apply(此,一个);
    }
});
 

解决方案

目前是全球 的ajaxSuccess 回调:

  

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

这会让你调用自己的函数在每一个成功的AJAX调用,而不与通常成功回调干扰。

有href="http://api.jquery.com/category/ajax/global-ajax-event-handlers/" rel="nofollow">全局AJAX事件处理其他各种

如果这些回调没有合适的时机,或者你的能力,那么你可以编写自己的包装 $ AJAX 并使用:

 函数wrapped_ajax(选项){
    VAR成功= options.success;
    options.success =功能(数据,textStatus,jqXHR){
        //做任何需要在这里完成。
        如果(成功)
            成功(数据,textStatus,jqXHR);
    };
    返回$阿贾克斯(选件);
}
 

您可以做任何你需要平时的成功回调的参数调用原始的成功回调之前。你会在完全相同的方式为 $叫 wrapped_ajax 。AJAX 。你可以使用同样的技术挂接到其他的回调也是如此。

I am trying to override the jQuery ajax function to handle a default action on a success event but also executing the callback function that i am using in the options parameter. What the purpose is there is tags returning in the response that I always want to strip out of the response for use elsewhere.

The scenario is:

  • Ajax submit
  • Ajax Success
  • --DEFAULT SUCCESS ACTION
  • --Call Ajax Success Callback

Can anyone help? I have tried extending

  • jQuery.ajax
  • jQuery.ajaxSuccess
  • jQuery.ajax.done

The code I have is:

var _ajaxSuccess = jQuery.fn.ajaxSuccess;  
$.fn.extend({  
    ajaxSuccess: function (a)  
    {  
        _ajaxSuccess.apply(this, a);  
    }  
});

解决方案

There is the global ajaxSuccess callback:

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.

That will let you call your own function on every successful AJAX call without interfering with the usual success callbacks.

There are various other global AJAX event handlers that you might want to look at too.

If those callbacks don't have the right timing or capabilities for you, then you could write your own wrapper for $.ajax and use that:

function wrapped_ajax(options) {
    var success = options.success;
    options.success = function(data, textStatus, jqXHR) {
        // Do whatever needs to be done here.
        if(success)
            success(data, textStatus, jqXHR);
    };
    return $.ajax(options);
}

You can do whatever you need to the usual success callback parameters before calling the original success callback. You'd call wrapped_ajax in exactly the same way as $.ajax. You could use the same technique to hook into the other callbacks as well.

这篇关于覆盖阿贾克斯成功事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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