I'm a big fan of the ASP.NET MVC framework. I love the fine grained control it gives you over everything. But what I love most is that you can unleash the fully armed and operational firepower of jQuery.
Recently, I've been working on a site that is using both MVC and jQuery to make AJAX calls. Typically the method being called returned HTML (via a PartialView). But, if an error was thrown, I wanted to return a JSON object encapsulating it.
Unfortunately, jQuery could not decide how to handle a response that could be either HTML, or a JSON object. You can tell jQuery what to expect, or let it try to figure out what the response dataType is. Here's the behavior of my Action method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult HandleAjax(int id) {
try {
var myObj = RetrieveData(id);
return PartialView("myview", myObj);
} catch (Exception ex) {
return Json(new { success = false, message = ex.Message });
}
}
The problem is that regardless of what gets returned, jQuery only recognizes it as a string, and treats it as HTML.
I did think of one solution, but it used pretty poor design: it attempted to parse the responseText as JSON. If that was successful, it meant the error object was returned. But, if an exception was thrown, it was the HTML. The problem is that it was using error handling mechanisms to capture an expected result. Bad news; I'm not even going to post the code.
Fortunately, someone pointed out that if I set the http status code to 500, jQuery will send the result to the error() callback function, as opposed to success(). Thus, if you added:
this.Response.StatusCode = 500;
In the catch block, you can then use jQuery's error() callback to do exactly as you'd expect:
$.ajax({
data: { id: 100 },
dataType: 'html',
error: function(XMLHttpRequest, textStatus, errorThrown) {
var errorObj = JSON.parse(XMLHttpRequest.responseText);
handleError(errorObj);
},
success: function(data) {
$('#resultContainer').html(data);
},
url: '/home/HandleAjax'
});
This also makes use of Crockford's JSON library to parse the responseText string to a JSON object.
It is this transparency that makes me sing the praises of the MVC framework. Such a mechanism is certainly possible with classic WebForms, but it is hidden under layers of abstraction, and likely would have required mixing of markup, script, and server side code.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5