ajax 请求登录超时跳转登录页的示例代码
Ajax
AJAX即“Asynchronous Javascript + XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。
在Filter里判断是否登录,如果未登录返回401状态
public class SelfOnlyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpResponseBase response = filterContext.HttpContext.Response;
HttpRequestBase request = filterContext.HttpContext.Request;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
//已经登录代码
}
else
{
if (!request.IsAjaxRequest())
{
string str = request.Url.AbsoluteUri.ToLower();
string str2 = "/login";
filterContext.Result = new RedirectResult(string.Format("{0}?returnUrl={1}", str2, str));
}
else
{
response.SetStatus(401);
}
filterContext.Result = new HttpUnauthorizedResult(); //这一行保证不再执行Action的代码
response.End(); //必须加上这句,否则返回前台status始终是200
return;
}
}
}
在全局js添加错误处理代码
$("div").ajaxError(function(e,xhr,opt){
alert(xhr.status+ " " + xhr.statusText);
if(xhr.status=401){
//TODO 未登录 跳转到登录页
window.location.href = ‘/login‘;
}
});
ajax 请求登录超时跳转登录页的示例代码的更多相关文章
- ajax 请求登录超时跳转登录页解决方法
在Filter里判断是否登录,如果未登录返回401状态 public class SelfOnlyAttribute : ActionFilterAttribute { public override ...
- iframe登录超时跳转登录页面
1.可以在登录页面加jQuery验证 $(function () { //判断一下当前是不是做顶层,如果不是,则做一下顶层页面重定向 if (window != top) { top.location ...
- Intent Flag实际项目 -- 超时跳转登录界面并清理前面所有activity
项目中涉及到登录超时跳转登录界面的逻辑,我以前的跳转flag为Intent.FLAG_ACTIVITY_CLEAR_TOP,但是点击返回按钮还是会回到上个界面.代码如下: ActivityUtils. ...
- 程序启动的目录不一样.ajax请求的地址跳转会出现的问题
程序启动的目录不一样.ajax请求的地址跳转会出现的问题启动 frontend/web/启动 frontend/ $.ajax({ url:"<?php echo Yii::$app- ...
- 【转载】Extjs设置Ajax请求的超时时间timeout
在Extjs中的Ajax请求中,Ext.Ajax.request 默认超时时间是30秒,有时候我们有比较耗时的操作需要设置更长时间,此时我们就需要修改Ext.Ajax.Requset的超时时间为更长, ...
- Spring Security登录超时,angular ajax请求出错自动跳转至登录页(jQuery也适用)
公司开发采用Spring Security+AngualerJS框架,在session过期之后,ajax请求会直接出错.本文介绍如何实现出错情况下自动跳转至登录页. 整体思路是,session过期后, ...
- dwz ajax session超时跳转登录页(struts2自定义拦截器)
1.定义struts2拦截器(网上例子很多) 代码如下: package rt.intercepter; import java.util.Map; import javax.servlet.http ...
- 处理jquery的ajax请求session过期跳转到登录页面
首先需要在拦截器中判断是否是ajax请求,如果是 if(isAjaxRequest(request)){//ajax请求 response.setHeader("sessionstatus& ...
- ajax请求session失效重定向到登录页面
在ajax请求的页面引入一个自定义的AjaxRedirect.js的文件 AjaxRedirect.js的代码如下: $(function(){ $.ajaxSetup({ type: 'POST', ...
随机推荐
- STDIN_FILENO vs stdin
数据类型不一致:stdin类型为 FILE*STDIN_FILENO类型为 int 使用stdin的函数主要有:fread.fwrite.fclose等,基本上都以f开头使用STDIN_FILENO的 ...
- UI_storyboard实现页面回调
新建类 注意继承关系 #import <UIKit/UIKit.h> @interface CustomPopIt : UIStoryboardSegue @end #import &qu ...
- activeMQ Jms Demo
概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经 ...
- HDU 4280Island Transport(网络流之最大流)
题目地址:pid=4280">http://acm.hdu.edu.cn/showproblem.php? pid=4280 这个题是一个纯最大流模板题..就是用来卡时间的.. 还好我 ...
- 常用RGB颜色表
作者:张家珩2005-12-02 20:51分类:默认分类 R G B 值 R G B 值 R G B 值 黑色 0 0 0 #000000 黄色 255 255 0 #FFFF0 ...
- <class 'Salesman.admin.UsrUserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[0][1]'.
ieldsets = ( ['Main', { 'fields': ('user_name', 'real_name', 'concat_name','phone_no','charge_person ...
- atitit.软件开发--socket框架选型--netty vs mina j
atitit.软件开发--socket框架选型--netty vs mina j . Netty是由JBOSS提供的一个java开源框架 Apache mina 三.文档比较 mina文档多,,, 好 ...
- 进程控制函数(3)-getsid()和setsid()获取当前会话和建立新会话
pid_t setsid(void) 1.调用进程不能是进程组组长,该进程变成新会话首进程(session header) 2.该进程成为一个新进程组的组长进程. 3.需有root权限(ubuntu不 ...
- removeFromParentAndCleanup和callfuncN_selector
void removeFromParentAndCleanup (bool cleanup)//删除父节点中的当前节点并清除动作及回调函数 void ActionCallFuncND::onEnte ...
- hdoj 1026 Ignatius and the Princess I 最小步数,并且保存路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...