Handling session timeout in ajax

别来无恙 提交于 2019-12-21 21:05:09

问题


In my MVC app I have the following attribute that checks for logged on users:

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")';
        });

but for some reason alert("in") never gets hit even when 403 code is thrown (from ajax request):


回答1:


Wrap the Ajax Error event in DOM Ready function

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

Or check here



来源:https://stackoverflow.com/questions/16080462/handling-session-timeout-in-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!