When writing Ajax data entry forms in my ASP.NET MVC3 app, I have a standard Ajax error handler, along the lines of:
function handleAjaxError(ajaxContext) {
var response = ajaxContext.get_response();
var statusCode = response.get_statusCode();
alert("Request failed, status code " + statusCode);
}
I'm now finding that the parameter sent to handleAjaxError isn't an Ajax context but the Response object itself, for some reason.
Is this a known behavior change in MVC3, maybe? Here's the form setup, if that's relevant:
@using (Ajax.BeginForm("Create", "Attendance", null, new AjaxOptions
{ OnFailure = "handleAjaxError",
OnSuccess = "alert('success')" },
new { id = "frmCreateException" }))
{
@Html.EditorFor(m => Model)
}
The controller action returns a PartialViewResult. The HTTP Exception at the moment is a 500, because I haven't yet created the view.
Thanks!
Is this a known behavior change in MVC3, maybe?
Yes, ASP.NET MVC 3 uses unobtrusive jQuery for AJAX stuff contrary to previous version which used Microsoft*.js
. So the first argument passed to the error handler is the jqXHR object.
And to get the response text and status code:
alert(ajaxContext.status);
alert(ajaxContext.responseText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With