How to run $ (elem) .ajaxSuccess () to $ .ajax ({success: ...}) ;?

what I'm trying to do is automatically run the function before moving on to the method $.ajax({success});. I have it:

$("body").ajaxSuccess(
    function(event, XMLHttpRequest, ajaxOptions) {
        alert("ajaxSuccess");
    }
);

and:

$.ajax({
    url: ".",
    success: function() {
        alert("success");
    }
);

My problem is that I see “success” first and then “ajaxSuccess” or would have the opposite. Is it possible? Any other solution?

The problem is the same: $ .ajaxError () ...

thank

+5
source share
1 answer

you can use the following :

dataFilter(data, type)

to intercept the request when it first returns from the server. This is not exactly what you wanted to use it for, but it is a universal preliminary procedure.

Pre-Error? , ?

var obj = {
   someHandler: function(data, xmlObj, status) {},
   someHandler2: function(data, xmlObj, status) {},
   someErrorHandler: function(xmlObj, status, error) {},
   someErrorHandler2: function(xmlObj, status, error) {}
}

jQuery.ajax({
   success:function(data, xmlObj, status) {
      // decide based on data what the run 2nd
      if (data.success) {
         obj.someHandler(data, xmlObj, status);
      }
      else {
         obj.someHandler2(data, xmlObj, status);
      }
   },
   error: function(xmlObj, status, error) {
      // decide based on data what to run 2nd
      //same as above.
   }
});

- . Error, TYPE , . AJAX

0

All Articles