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();
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";
}
}
MVC Ajax调用Action时-OnActionExecuting RedirectResult 无法跳转的处理办法的更多相关文章
- 爱上MVC~ajax调用分部视图session超时页面跳转问题
回到目录 这个问题出现了很多年了,都没有解决,问题是这样的,有一个需要授权才可以访问的分部视图,在一个view中使用ajax的方法去调用它,然后更新页面的局部DIV,这时,如果你长时间不操作,sess ...
- mvc ajax访问后台时session过期无法跳转到Login页面问题解决
public class BaseController : Controller { protected User UserInfo { set { Session["UserInfo&qu ...
- asp.net mvc 使用Ajax调用Action 返回数据【转】
使用asp.net mvc 调用Action方法很简单. 一.无参数方法. 1.首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择. <script src=& ...
- Asp.Net MVC ajax调用 .net 类库问题
如果你还在为 ajax 调用 .net 类库还束手无策的话,相信这篇博客将帮助你解决这个世纪问题! 因为Visual Studio 内置了asp.net mvc ,不过当你添加asp.net mvc项 ...
- Struct2_使用Ajax调用Action方法并返回值
一.Login.jsp 1.<head>引入jquery: <script type="text/javascript" src="http://aja ...
- Spark为什么只有在调用action时才会触发任务执行呢(附算子优化和使用示例)?
Spark算子主要划分为两类:transformation和action,并且只有action算子触发的时候才会真正执行任务.还记得之前的文章<Spark RDD详解>中提到,Spark ...
- 使用jQuery的ajax调用action的例子
直接使用ajax请求会比较繁琐,但是jQuery为我们提供了简单使用ajax的方法. 下面是一个在jQuery easyUI中,利用ajax请求,使下拉菜单关联文本框的例子.其中ajax请求就是8-1 ...
- 通过Web API调用Action时各种类型输入参数传递值的方法
本人微信公众号:微软动态CRM专家罗勇 ,回复280或者20180906可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . Dy ...
- SSH框架下ajax调用action并生成JSON再传递到客户端【以get和post方式提交】
需要完成的任务: 主要是把JSP页面上图片ID传给服务器端,服务器读取cookie看是否有username,如果有则根据ID读取MongoDB数据库,读出图片URL,再存放到mysql中的collec ...
随机推荐
- androidannotations的background和UiThread配合使用參考
简单介绍 androidannotations在开发中的代码规范思考:(MVC思考)时间太紧,先贴代码: Activity的代码: package edu.njupt.zhb.main; import ...
- CSS中的BFC解析
CSS的BFC BFC 即块级格式上下文(Block Formatting Context),它是指一个独立的块级渲染区域,只有block-level的box参与,该区域拥有一套渲染规则来约束块级盒子 ...
- 转 java中5种创建对象的方法
作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如spring去创建对象.然而这里有很多创建对象的方法,我们会在这篇文章中学到. Java中有5种创建对象的方式,下面给出它们的 ...
- 转 java面试题
● 简述synchronized?Object:Monitor机制: ● 简述happen-before规则 : ● JUC和Object : Monitor机制区别是什么 : 简述AQS原理 : ● ...
- 阿里云部署Docker(2)
之前有一篇文章讲过在阿里云中安装Docker,相对来说那个是安装.可是安装完之后我们通常会碰到问题. 今天我给大家记录一下我的新的解决过程. 环境还是ubuntu12.04.如果我们已经把内核升级到了 ...
- vim入门级使用
1.刚进入是 command mode 命令行模式 2.i 进入插入 insert mode 模式 在插入模式下只能 输入内容,如果要删除内容,需要切换到命令行模式,移动光标进行删除. 3.esc ...
- ios11--UIButton
// // ViewController.m // 02-UIButton(在代码中使用) // #import "ViewController.h" @interface Vie ...
- 安装nghttp2 报错error: Libtool library used but 'LIBTOOL' is undefined
nghttp2 报错error: Libtool library used but ‘LIBTOOL‘ is undefined 如果重新安装libtool和autoconf升级到2.69后,还是报错 ...
- Kafka VS Flume
(1)kafka和flume都是日志系统.kafka是分布式消息中间件,自带存储,提供push和pull存取数据功能.flume分为agent(数据采集器),collector(数据简单处理和写入) ...
- 【WIP】Bootstrap nav
创建: 2017/09/28 更新: 2017/10/14 标题加上[WIP]