Skip to main content
 首页 » 编程设计

c#之使用 MVC 3 不显眼的 Javascript 进行全局 jQuery AJAX 错误处理

2024年01月12日33jillzhang

我的 ajax 调用有一个全局错误处理程序,如下所示:

$.ajaxSetup( 
{ 
    error: function(XMLHttpRequest, textStatus, errorThrown) 
    { 
        alert("error"); 
    } 
}); 

我正在像这样进行 ajax 调用:

@Ajax.ActionLink("Submit", "SubmitQuery", new AjaxOptions { HttpMethod = "POST" }) 

在 Controller 操作上,我有一个返回的 CustomAuthorizeAttribute:

filterContext.Result = new HttpStatusCodeResult(403, "adsfasd"); 

当用户未登录时。

出于某种原因,我的全局 jQuery AJAX 错误处理程序没有捕获 403 错误。

请您参考如下方法:

我设法弄明白了。

我需要:

filterContext.Result = new ContentResult 
{ 
    ContentType = "text/plain", 
    Content = "Access Denied. Invalid user name and/or password." 
}; 
 
filterContext.HttpContext.Response.Status = "500 ".Replace("\r", " ").Replace("\n", " ");