我有关于客户端异常抛出和捕获的问题。在我介绍HTTP401之前,这一切都运行良好。这是我的基本控制器,而不是覆盖OnExcpetion:
protected override void OnException(ExceptionContext filterContext)
{
var exception = HandleException.Handle(filterContext.Exception);
filterContext.Exception = exception;
if(exception is AuthenticationException)
filterContext.HttpContext.Response.StatusCode = 401;
else
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.ExceptionHandled = true;
filterContext.Result = new JsonResult()
{
Data = new { Message = filterContext.Exception.Message },
ContentType = "json"
};
}
这是我的AJAX调用以及成功和错误。
$.ajax(
{
type: 'POST',
url: loginRoute,
data: formData,
dataType: 'json',
processData: false,
contentType: false,
statusCode: {
401: function () {
$('#authenticationErrorMessage').text(xhr.responseJSON.Message);
$('#authenticationError').show();
}
},
success: function (responseData) {
$('#authenticationSucessMessage').text(responseData.Message);
$('#authenticationSuccess').show();
window.location.replace(responseData.Redirect);
},
error: function (xhr, ajaxOptions, thrownError) {
$('#authenticationErrorMessage').text(xhr.responseJSON.Message);
$('#authenticationError').show();
}
});
问题是完成后都不会调用状态代码和错误。只有成功才被称为。如果我将StatusCode设置为500,则一切正常。
答案 0 :(得分:0)
您可以全局处理它。可能会低于代码帮助你。
$( document ).ajaxError(function( event, jqxhr, settings, exception ) {
if ( jqxhr.status== 401 ) {
alert( "Triggered ajaxError handler." );
}
});