I have many $.post, $.ajax call, they all returns with a JSON array. I realized that sometimes I have to check status codes, e.g. check if user is dropped. Thats ok, but now I have to put this status-check for all occurrences. By accindent, cant I set a default callback somehow?
3 Answers
You can use:-
Description: Attach a function to be executed before an Ajax request is sent. This is an Ajax Event. Whenever an Ajax request is about to be sent, jQuery triggers the ajaxSend event. Any and all handlers that have been registered with the .ajaxSend() method are executed at this time.
.ajaxSend( handler )
Type: Function( Event event, jqXHR jqXHR, PlainObject ajaxOptions ) The function to be invoked.
Usage:-
$(document).ajaxSend(function( event, jqxhr, settings ) {
// This will be fired before all ajax requests
});
NOTE
If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxStart() method will not fire.
beforeSendoption? api.jquery.com/jquery.ajax