Extending jQuery ajax success globally

后端 未结 5 1911
庸人自扰
庸人自扰 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:49

    this is your call to ajax method

     function getData(newUrl, newData, callBack) {
               $.ajax({
                   type: 'POST',
                   contentType: "application/json; charset=utf-8",
                   url: newUrl,
                   data: newData,
                   dataType: "json",
    
                   ajaxSuccess: function () { alert('ajaxSuccess'); },
                   success: function (response) {
                       callBack(true, response);
                       if (callBack == null || callBack == undefined) {
                           callBack(false, null);
                       }
                   },
                   error: function () {
                       callBack(false, null);
                   }
               });
           }
    

    and after that callback success or method success

    $(document).ajaxStart(function () {
                   alert('ajax ajaxStart called');
               });
               $(document).ajaxSuccess(function () {
                   alert('ajax gvPerson ajaxSuccess called');
               });
    

提交回复
热议问题