how to overwrite the success function via JQuery ajaxSend event

Viewed 2247

I am trying to overwrite the success function upon ajaxsend event but it doesnt work here is the code:

    $(document).ajaxSend(function(event,xhr,options){
        console.log('ajaxSend');
        var tempSuccess = options.success;
        options.success = function(data, textStatus, jqXHR){
            console.log('start');
            tempSuccess(data, textStatus, jqXHR);
            console.log('end');
        }; xhr.success = options.success;});

upon AJAX I do see 'ajax' in the console, but upon success I can't see the start and the end debug msges..

What do I do wrong?

3 Answers

Because the option.success is used before ajaxSend. When it was used,it was added to the Deferred Object.

After that,you change the option.success, nothing will happen.

So, you must change it before is was used. the process is:

$.ajaxPrefilter -- $.fn.ajaxStart(if needed) -- option.beforeSend -- 
USE option.success and option.error and option.complete -- 
$.ajaxTransport -- $.fn.ajaxSend -- send the actual request -- 
option.dataFilter --  fire the success/error callbacks -- 
$.fn.ajaxSuccess/$.fn.ajaxError -- fire the complete callbacks --  
$.fn.ajaxComplete -- $.fn.ajaxStop(if needed)

So, you can change the option.success in "$.ajaxPrefilter" or "option.beforeSend".

Related