Using AJAX has become one of the de facto practices in many web based RIAs. The use of jQuery is on the rise in many web applications, especially the ones built using ASP.NET MVC. Making AJAX calls using jQuery is quick and easy. A typical web request can have an expected response or an exception. It is therefore important to catch exceptions when we make any AJAX calls so that we can show some message which makes sense to the user.
Lets begin with our Controller. Create the following method to throw JSON version of the exception.
1 private JsonResult ThrowJSONError(Exception e)
2 {
3 Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
4 return Json(new { Message = e.Message, StackTrace = e.StackTrace });
5 }
Specifying the StatusCode as HttpStatusCode.BadRequest or HttpStatusCode.InternalServerError or simply 500 will let the javascript know that there is something wrong in the response.
Lets create an ActionMethod to throw an exception which we will catch using jQuery.
01 public ActionResult DivideByZero()
02 {
03 try
04 {
05 throw new DivideByZeroException();
06 }
07 catch (DivideByZeroException e)
08 {
09 return ThrowJSONError(e);
10 }
11
12 }
The above method will throw a DivideByZeroException which will be converted to a JSON string in the catch block and returned to the caller. Lets write the jQuery code to call the ActionMethod.
01 $(document).ready(function() {
02 $.ajax({
03 type: "GET",
04 url: "AJAX/DivideByZero",
05 dataType: "json",
06 success: function(data) {
07 if (data) {
08 alert("Success!!!");
09 }
10 }, error: function(xhr, status, error) {
11 DisplayError(xhr);
12 }
13 });
14 });
15
16 function DisplayError(xhr) {
17 var msg = JSON.parse(xhr.responseText);
18 alert(msg.Message);
19 }
Here we are making a call to the DivideByZero ActionMethod. Obviously the exception will be thrown,which will be caught in the error block. The responseText is a string of serialized data, which will not be useful unless we parse it to JSON. We can use the native JSON parser available in the latest browser. But, this as I found out is not helpful in IE 7 or lower. Fortunately, there is a useful library at JSON.org that can parse the responseText to JSON.
Original Source:http://2leggedspider.wordpress.com/2009/12/22/handling-exceptions-using-jquery-and-asp-net-mvc/
Related Stuff
-
MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.
-
Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone
-
TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support
-
ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.
-
Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.
-
Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software
- PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.
