views:

2834

answers:

2

I'm working on a stripes app that uses a bit of jQuery to make the UI more dynamic/usable.

I set up an Error Resolution, so if an error is thrown, the user is redirected to an error.jsp page.

However, if an error is thrown during a jQuery Ajax call, instead of redirecting to the error.jsp page, I get html printed to the page where the result of the call should have been instead.

How do I tell jQuery to redirect if an exception was thrown instead of printing to the page?

An example of the offending Ajax:

$.post("SendStatusEmail.action",
      {status: newstatus, id : id },
      function(data) {
       column.text(data);
       column.addClass("redfont");
       column.parent().fadeOut(3000, function(){column.parent().remove()});
+3  A: 
$(document).ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError) {
    // redirect here.
}

I should add that I don't redirect when there is an exception in an Ajax call. Instead, I have the server return an error description in JSON format and display that in the page.

Craig Stuntz
Well.. actually the server is handling a thrown Exception, so it's spitting back some html instead of the plain text I was expecting, so jQuery thinks everything went OK. I guess to use this I could make the exception handler return a 500 HTTP status code or something? hmm..
lucas
Right, 500 = ajaxError called.
Craig Stuntz
+4  A: 

What about using $.ajax() instead of $.post()? The more general $.ajax() offers an error callback that you can call in case of an error.

kgiannakakis