MVC 3 Ajax.ActionLink How to get the status code in the onFailure handler?
In the action called by an Ajax action link, I need to throw an HttpException based on the current c开发者_StackOverflow中文版onditions. Depending on those conditions, the HttpException has a different code.
How do I get the status code in the onFailure handler?
I had the same problem, and I figured out you do not need to throw an HttpException. Instead do:
return new HttpStatusCodeResult(401);
The onFailure handler should look like this:
function handleOnFailure(response) {
alert(response.status);
}
For me the only problem is now how to handle the 401 myself instead of MVC showing the logon page. In case of a 401 the alert is not shown, but with any other statuscode this works.
Simply set AjaxOptions.OnFailure
property to a function like this:
<script>
function onFailure(xhr, status) {
alert(xhr.statusText);
}
</script>
See this post: https://www.experts-exchange.com/questions/28583187/MVC-Ajax-BeginForm-how-to-handle-OnSuccess-and-OnFailure-from-Controller.html
精彩评论