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