JQuery帖子上的调用事件

I have a full ajax application. Many buttons and textfields trigger $.post methods. Now, what I want to do is to check every post's method result and if this object has success field set to false, I want to execute some_method method. What is the best way to do it? I don't want to do this check in every function where I use post method. Is it possible to create some observer which gets triggered on every post method's success? Thank you.

Use the ajaxSuccess() global event handler

Attach a function to be executed whenever an Ajax request completes successfully

Ex:

$('body').ajaxSuccess(function(e, xhr, options) {
    if (xhr.responseText 
        && options.dataType == "json") {
        var json = JSON.parse(xhr.responseText );
        //custom
    }
});

You can use this:

$(document).ajaxSuccess(function(event, request, settings) {
  if (settings.type == "POST") {
   console.log("Post success :D");
  }
});

$.post(url, function(data) {
 console.log("Success return: " + data);
}).fail(function(e) {
 console.log("Error: " + e.message);
});

You will have to use the ajaxSuccess global event handler and add some logic to prevent executing both callbacks:

$(document).ajaxSuccess(function(event, xhr, settings) {
  if ( !settings.success ) {
    //The ajax request does not have a success callback
  }
});

Or you have to change every post call to set the parameter global to false to prevent the global handlers from being triggered.