Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax.BeginForm OnFailure invoked with Response, not AjaxContext

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!

like image 757
Graham Charles Avatar asked Dec 22 '22 08:12

Graham Charles


1 Answers

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);
like image 183
Darin Dimitrov Avatar answered Mar 15 '23 13:03

Darin Dimitrov