在我的MVC应用程序中,我有以下属性来检查已登录的用户:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
....
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new Http403Result();
filterContext.HttpContext.Response.StatusCode = 403;
}
...
}
Layout.cshtml
$("body").ajaxError(
function (e, request, settings, exception) {
alert('in');
if (request.status == 403) {
window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash;
return;
}
if (request.status == 500) {
window.location = '@Url.Action("AccessDenied", "Error")';
return;
}
if (request.status == 410) {
window.location = '@Url.Action("Deleted", "Error")';
return;
}
window.location = '@Url.Action("Index", "Error")';
});
但由于某种原因,即使抛出403代码(来自ajax请求),alert(“in”)也永远不会被击中:
答案 0 :(得分:4)
在DOM Ready函数中包装Ajax错误事件
$(document).ready(function () {
$(document).ajaxError(function (e, xhr, settings) {
debugger;
});
});