0

I find myself putting this (simplified) code in a number of different javascript files:

 $(document).ajaxError(function (e, xhr, settings, exception) {
     alert('error in: ' + 
                         settings.url + ' \\n' + 
                         'error:\\n' + exception + 
                         ": " + xhr.responseText
          );
 });

is there any downside that someone can think of by just putting this in my Site.Master file once so I have a consistent solution to errors?. If I do that, is there anyway to override the behavior on a specific page?

1
  • showing the url or the exception to the end user does not make any sense... just show the error message from the server Commented Aug 26, 2013 at 16:47

1 Answer 1

1

Use $(document).ajaxError in your Site.Master file.

To override this in a specific instance, then just include the error callback on individual AJAX calls, like this:

$.ajax({
    url: '/yourUrl',
    success: function(result) {
        alert('It worked!');
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert('Your custom error message here.');
    }
});

For those that you do not want to override, them omit the error callback.

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

2 Comments

out of curiosity, What is the difference between the ajaxSetup() versus just having the $(document).ajaxError call directly as I have in the above question?
Wow, just read the documentation for this and it appears that using jQuery .ajaxSetup({error:{); is considered a bad practice and it is encouraged to use the $(document).ajaxError handler instead, I am updating my answer to reflect that.

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.