My question is what is condition for the OnFailure callback to be called , how does the runtime know the ajax call is failed (the ajax helper use some http response status code to indicate that? what it would be then?). And if the html of UpdateTargetId is updated no matter the ajax call is failed or success, then how should I handle the error properly then. Very confused...
<script type="text/javascript">
function OnSuccess() {
alert('Success');
}
function OnFailure(ajaxContext) {
var response = ajaxContext.get_response();
var statusCode = response.get_statusCode();
alert('Failure');
Here you can do whatever you want with the div.
$('#targetDiv').empty();
}
</script>
<div id="targetDiv">
@using (Ajax.BeginForm("Index", "Home",
new AjaxOptions
{
UpdateTargetId = "targetDiv",
OnSuccess ="OnSuccess",
OnFailure ="OnFailure"
})
{
...
}
</div>
It seems that in ASP.NET MVC 4 things had changed a little. I had to use the following properties to read the response and status:
ajaxContext.responseJSON
ajaxContext.responseText
ajaxContext.status
ajaxContext.statusText
According to the official MSDN website: This function is called if the response status is not in the 200 range.
AjaxOptions.OnFailure Property
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