For example I have general code for catching json, that says – “its error request”. This code is in ajaxComplete, how can I stop executing specific code from ajaxComplete point?
$.post('url', params,
function(json){
if (json.success == false){
alert('error')
}
if (json.success == true){
alert('success')
}
}, 'json'
);
Instead of this code in each ajax request, I want to use something like:
$().ajaxComplete(function(e, xhr){
if (json.success == false){
stop_execution_of_post()
}
if (json.success == true){
proceed_execution_of_post();
}
}
Than in post, you would only to write this:
$.post('url', params,
function(json){
alert('success')
}, 'json'
);
Is that possible to STOP from execution SPECIFIC ajax function?
Instead of using
ajaxCompleteyou can override an internal method of jQuery. Obviously you need to be careful because the internal method may change. This is written against jQuery 1.4.The method is jQuery’s
httpSuccess(xhr). This takes anXMLHTTPRequestobject. When it returnsfalsejQuery’s AJAX error handler is called (handleError). Most notably thesuccesshandler is not called. Note that thecompletehandler is always called.An example:
So whenever our
weNeedToStoptest is true, for any ajax response, the success handler is not called.