• MVC Ajax调用Action时OnActionExecuting RedirectResult 无法跳转的处理办法


    public class BaseController : Controller
        {
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (Session["userInfo"] == null)
                {
                    if (filterContext.HttpContext.Request.IsAjaxRequest())
                    {
                        filterContext.Result = new HttpStatusCodeResult(499);
                        filterContext.HttpContext.Response.Write("/Desktop/Login/Index");
                        return;
                    }
                    else
                    {
                        filterContext.Result = new RedirectResult("/Desktop/Login/Index");
                        return;
                    }
                }
                base.OnActionExecuting(filterContext);
            }
        }
    }

    (function ($) {
    //有可能导致jquery里面的done方法未定义;所以有时候可以全部注释掉;
    var _ajax = $.ajax; $.ajax = function (opt) { var fn = { error: function (XMLHttpRequest, textStatus, errorThrown) { }, } if (opt.error) fn.error = opt.error; var _opt = $.extend(opt, { error: function (XMLHttpRequest, textStatus, errorThrown) { if (XMLHttpRequest.status == "499") window.location.href = "/Desktop/Login/Index"; else fn.error(XMLHttpRequest, textStatus, errorThrown); } }); _ajax(_opt); }; })(jQuery); $.ajaxSetup({ statusCode: { 499: function (data) { window.location.href = data.responseText; } } });

    $.ajax({
                    type: "POST",
                    url: "/desktop/language/index",
                    data: { language: getCookie("languageCode") },
                    datatype: "json",
                    success: function (returndata) {
                        //alert("second success");
                    }
                });

    //如果是Ajax.BeginForm,跳转到登陆页方式为如下:

    @using (Ajax.BeginForm("GetSystemStatusTracking", "ReportQuery",
      new AjaxOptions() { UpdateTargetId = "TRACKING_NEWLIST",
        OnBegin = "showModal",
        OnComplete = "hideModal",
        OnFailure = "failureProcess"
      }))
    {

    }

    function failureProcess(data) {
    if (data.status == 499)
    window.location.href = (data.responseText == '') ? window.location.href = "/Desktop/Login/Index" : data.responseText;
    }

     
    
    

     特别注意:当用IIS发布时,客户端访问会报错“ 自定义错误模块不能识别此错误”,但调试时和本机发布本机访问都不会报错;

     初步怀疑是服务器和客户端字体解析问题导致。代码需改为如下:

    $.ajaxSetup({
      statusCode: {
        499: function (data) {
          window.location.href = "/Desktop/Login/Index";
        }
      }
    });

    
    

    function failureProcess(data) {
      if (data.status == 499) {
        window.location.href = "/Desktop/Login/Index";
      }
    }

     
    
    
    
     
  • 相关阅读:
    LintCode-Search for a Range
    LintCode-Serialization and Deserialization Of Binary Tree
    LeetCode-Reverse Words in a String
    LeetCode-Reorder List
    LeetCode-Word Break
    LeetCode-Word Ladder
    LeetCode-Valid Palindrome
    cf div2 237 D
    POJ 1759
    cf div2 238 D
  • 原文地址:https://www.cnblogs.com/turnip/p/10818657.html
Copyright © 2020-2023  润新知