【问题标题】:Handling session timeout in ajax在ajax中处理会话超时
【发布时间】:2013-04-18 10:27:01
【问题描述】:

在我的 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") 也不会被命中:

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-4


    【解决方案1】:

    在 DOM Ready 函数中包装 Ajax 错误事件

    $(document).ready(function () {
        $(document).ajaxError(function (e, xhr, settings) {
            debugger;
        });
    });
    

    check here

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 2017-12-30
      • 2011-07-11
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2013-06-10
      • 2013-01-20
      相关资源
      最近更新 更多