对于ajax提交session超时跳转页面,全局感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍ajax提交请求超时,并为您提供关于ajaxsession失效后,跳转到登录页面的全局处理、aja
对于ajax提交session超时跳转页面,全局感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍ajax提交请求超时,并为您提供关于ajax session失效后,跳转到登录页面的全局处理、ajax session过期 页面跳转问题、ajax 登录session失效登录页面跳转 ajax扩展来做、ajax不能跳转页面,需要传递Message来决定结果的有用信息。
本文目录一览:- ajax提交session超时跳转页面,全局(ajax提交请求超时)
- ajax session失效后,跳转到登录页面的全局处理
- ajax session过期 页面跳转问题
- ajax 登录session失效登录页面跳转 ajax扩展来做
- ajax不能跳转页面,需要传递Message来决定结果
ajax提交session超时跳转页面,全局(ajax提交请求超时)
在过滤器中写入如下方法:
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,servletexception { HttpServletRequest httprequest = (HttpServletRequest) request; HttpServletResponse httpresponse = (HttpServletResponse) response; String url = httprequest.getRequestURL().toString(); if (httprequest.getSession()== null) { if (httprequest.getHeader("x-requested-with") != null && httprequest.getHeader("x-requested-with").equals( "XMLHttpRequest")) { // ajax请求 httpresponse.setHeader("sessionstatus","timeout"); } else { httpresponse.sendRedirect("/test/index.jsp"); return; } } else { chain.doFilter(request,response); } }
这样,如果session超时,而且是ajax请求,就会在响应头里,sessionstatus有一个timeout;
再用一个全局的方法来处理,session超时要跳转的页面。
jquery可以用$.ajaxSetup方法,ext也有类似的方法:
//全局的ajax访问,处理ajax清求时sesion超时 $.ajaxSetup({ contentType : "application/x-www-form-urlencoded;charset=utf-8",complete : function(XMLHttpRequest,textStatus) { var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus"); // 通过XMLHttpRequest取得响应头,sessionstatus, if (sessionstatus == "timeout") { // 如果超时就处理 ,指定要跳转的页面 window.location.replace("/test/index.jsp"); } } });
ajax session失效后,跳转到登录页面的全局处理
在SaaS系统中,我们需要考虑,用户停留页面时间过长导致session失效后,ajax方法无法正确运行,我们又不希望在每个ajax方法中,来判断是否登录,未登录的情况下就跳转到登录页.
我们的解决方案是:
首先,有一个Intercepter 实现了handlerinterceptor接口.
在preHandler方法中,判断handler对象类型,我们只处理 spring controller方法.
if (handler instanceof HandlerMethod) { // intercept Account account = (Account) session.getAttribute("account"); if (account == null) { HandlerMethod handlerMethod = (HandlerMethod) handler; // 根据请求的是否是ajax方法,来判断是直接302还是返回一个JSON if (handlerMethod.hasMethodAnnotation(ResponseBody.class)) { // ajax方法 PrintWriter printWriter = response.getWriter(); response.setStatus(499); String url = request.getHeader("referer"); if (org.springframework.util.StringUtils.isEmpty(url)) { url = loginPath; } printWriter.print(url); } else { String redirectUrl = loginPath; session.setAttribute("retUrl",request.getRequestURL().toString()); response.sendRedirect(redirectUrl); } return false; } else { List<Role> roleList = (List<Role>) session.getAttribute("roles"); SessionHolder.setAccount(account); SessionHolder.setRoles(roleList); return true; } }
在前端页面加入:
$.ajaxSetup({ statusCode: { 499: function (data) { window.location.href = data.responseText; } } });
ajax session过期 页面跳转问题
在WEB应用中,一般通过监控SESSION来判断用户是否登录、或是否发呆时间过长。如果SESSION过期或用户未登录,用户再次向服务器请求资源的时候,就跳转(重定向)到登录页面。可实际上,用户在请求资源的时候,页面的状态可能有多种:带框架的页面、window.open()函数打开的窗口。这时候跳转(重定向)到登录页面就会有问题。其实可以拿过一个简单的脚本来解决这个问题:
<script type="text/javascript">
if(window.opener){
//若是弹出的打开窗口,刷新父窗口,就关闭本窗口
window.open.reload();
window.close();
}
else{
var topwin = window.parent;
//找到顶层窗口
while(topwin!= topwin.parent){
topwin = topwin.parent;
if(window.parent!=window)
topwin.location.href=
"${pageContext.request.contextpath}/index. jsp ";
</script>
http://www.2cto.com/kf/201109/103416.html
ajax 登录session失效登录页面跳转 ajax扩展来做
今天过的时间有点久了以后,去点击layer查询的时候,提示接口失效
其实是session会话失效了。layer去请求的时候,经过拦截器,经过判断。session会话失效了。返回一段登录页面跳转。但是ajax不能执行跳转。所以经过前端调试,能看到一段response响应
那针对这种问题,要怎么做呢?百度以后得知,可以用ajax扩展来做。要不然的话,就只得改变每个的ajax的方法。但是这种也不太实现呀。太麻烦了。所以用ajax扩展来做
了解一下用法:
(function($){….})(jQuery)定义了一个匿名函数,然后又调用该函数,并传递实参jQuery
$.extend去扩展原生的jquery.ajax为jQuery类本身扩展,添加新的方法或覆盖原有的方法
ajax不能跳转页面,需要传递Message来决定结果
@RequestMapping(value = "/updateEmployeeIntro",method = RequestMethod.POST)@ResponseBody
public Message updateEmployeeIntroById(HttpSession session,EmployeeIntroVO employeeIntroVo) {
Message msg = new Message();
EmployeeByCodeAndPwdVO employee = (EmployeeByCodeAndPwdVO) session.getAttribute("employee");
employeeIntroVo.setEmployeeId(employee.getId());
employeeIntroVo.setUpdator(employee.getId());
employeeIntroVo.setUpdateTime(new Date());
int retNumber = employeeBaseService.updateEmployeeDetail(employeeIntroVo);
if (retNumber > 0) {
msg.setResult(true);
} else {
msg.setResult(false);
}
return msg;
}
$("#submitProcess").on("click",function(event){ event.preventDefault();//使a自带的方法失效,即无法调整到href中的URL(http://www.baidu.com) var serialNumber=$("#serialNumber").val(); var organizationLevelName = $("#organizationLevelName").val(); var employeePosts = $("#employeePosts").val(); var employeeNickName = $("#employeeNickName").val(); var employeeCode = $("#employeeCode").val(); var reason = $("#borrowReason").val(); var borrowMoney = $("#borrowMoney").val(); var applicationDate = $("#applicationDate").val(); var repaymentDate = $("#repaymentDate").val(); //前台检查 if(reason == "" && reason==null) { alert("Apply Reason Can not be empty"); } if(borrowMoney == 0 && borrowMoney == null) { alert("Apply Money Can not be empty"); } if(applicationDate == null) { alert("applicationDate Can not be empty"); } if(repaymentDate == null) { alert("repaymentDate Can not be empty"); } $.ajax({ type: "POST",url: basePath + "workflow/oa/costBorrow/startProcess.htmls",data: { "serialNumber":serialNumber,"organizationLevelName": organizationLevelName,"employeePosts": employeePosts,"employeeNickName": employeeNickName,"employeeCode" : employeeCode,"applicationDate" : applicationDate,"repaymentDate" : repaymentDate,"reason" : reason,"money" : borrowMoney },dataType:"json",success: function(result){ //请求正确之后的操作 // alert(result.msg); // alert("sucess"); if("loginAgain" == result.msg) { setTimeout(function(){ window.location=basePath+"admin/employee/login.htmls"; },100); } else { setTimeout(function(){ window.location=basePath+"workflow/process/start.htmls"; },100); } /* */ },error: function(result){ //请求失败之后的操作 alert("fail"); } }); });关于ajax提交session超时跳转页面,全局和ajax提交请求超时的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于ajax session失效后,跳转到登录页面的全局处理、ajax session过期 页面跳转问题、ajax 登录session失效登录页面跳转 ajax扩展来做、ajax不能跳转页面,需要传递Message来决定结果的相关信息,请在本站寻找。
本文标签: