If I use jQuery ajax with an error handler and the ASP.NET MVC action that I’m invoking throws an exception, I’d like to be able to display the message in the exception to the user. Right now I’m using:
$.ajax( { ... error: function(request,status,error) { var exp = new RegExp('<title>(.*)<\/title>','i'); if (exp.exec(request.responseText)) { alert( RegExp.lastParen ); } else { alert( status ); } } });
I’m hoping for a canonical jQuery implementation, rather than rolling my own.
For what it’s worth, I usually have any action that returns a JsonResult wrapped in a try/catch block so that it cannot throw an exception. Instead, I return two parameters with every Json array: ‘ok’ (true/false) and ‘message’ (containing the message to display to the user if there is an error).
Then I omit the $.ajax ‘error’ parameter, and do a simple branch on the ‘ok’ parameter of the JsonResult result.