1

I have many places in my code doing "$.ajax" call can I define one place to handle all errors ?

I know there is "error: function ... " that I can place in each call , but I want to write it just one time

4 Answers 4

4

How 'bout the ajaxError event?

Sign up to request clarification or add additional context in comments.

Comments

1
$.ajaxSetup({});

3 Comments

How very...terse. @OP: It sets default options for Ajax requests. Link: docs.jquery.com/Ajax/jQuery.ajaxSetup#options
$.ajaxSetup({ error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest + ':' + textStatus + ':' + errorThrown); return false; } }); is this OK ?
@haddar: There's sample code in the docs I linked (click the "Examples" tab). But that looks about right, yeah.
1

You should not use the $.ajaxSetup() event handler method (as stated by the JQuery docs):

http://api.jquery.com/jQuery.ajaxSetup/

Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().

Use the $.ajaxError() method instead:

http://api.jquery.com/ajaxError

Comments

0

You can define your error handling function just once and then refer to it in the Ajax call rather than using function literals in each call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.