Extending jQuery ajax success globally

后端 未结 5 1935
庸人自扰
庸人自扰 2021-01-03 03:19

I\'m trying to create a global handler that gets called before the ajax success callback. I do a lot of ajax calls with my app, and if it is an error I return a specific st

5条回答
  •  既然无缘
    2021-01-03 03:42

    This solution transparently adds a custom success handler to every $.ajax() call using the duck punching technique

    (function() {
        var _oldAjax = $.ajax;
        $.ajax = function(options) {
            $.extend(options, {
                success: function() {
                    // do your stuff
                }
            });
            return _oldAjax(options);
         };
    })();
    

提交回复
热议问题