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

标签 c# jquery asp.net-mvc

我的 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", " ");

关于c# - 使用 MVC 3 不显眼的 Javascript 进行全局 jQuery AJAX 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211908/

相关文章:

c# - 一路反序列化成dynamic?

c# - Contextmenu 的 PlacementTarget 未设置

asp.net - 发现了多种与名为 'Home'的 Controller 匹配的类型。 (两个区域,相同的 Controller 名称)

javascript - 使用 javascript 对输入字段中的所有值求和

jquery - 如何向 Colorbox 模态窗口添加填充?

c# - 使用中的错误案例列表 _userManager.CreateAsync(user, password)

c# - 如何在 Asp.Net MVC 4 中模拟 HttpContext.User.Identity.Name

C# 控制台应用程序点效果

c# - Linq 选择集合中的第一个事件记录

jQuery 工具提示跟随鼠标