Filippo oretti
Filippo oretti

Reputation: 49843

Extending jQuery for recursive ajax method

i would like to know if is possible to generate a method/extension/change for the jQuery lib to specify for all $.ajax() calls a method to be executed for example in timeout:, or in beforeSend:, statments

Upvotes: 0

Views: 447

Answers (1)

Saeed Neamati
Saeed Neamati

Reputation: 35842

Of course. There are many ways, but one of the simple methods for having a centralized method to be run on every ajax call, is to wrap jQuery ajax in your custom ajax wrapper.

(function ($){
    $.customAjax = function(path, data, successCallback, errorCallback){
        function errorFallback(){
             // Here, do what you want to do on any ajax call, which doesn't have error     callback
        };
        errorCallback= errorCallback|| errorFallback;
        $.ajax({
              // Calling the jQuery ajax, passing either specified error callback or a   default callback.
        });
    };
})(jQuery);

Upvotes: 2

Related Questions