超时重新请求 ajax,超时下Ajax请求处理

public class UserLogin extends HttpServlet

{

/** serialVersionUID */

private static final long serialVersionUID = 464654165487455L;

/**

* 这里只是示例代码,没有考虑代码的合理性:

*

* The doGet method of the servlet.

* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

// 如果是ajax请求,响应头会有x-requested-with相关字段信息

if (null == request.getSession().getAttribute("user"))

{

if (request.getHeader("x-requested-with") != null

&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))

{

//在响应头设置session状态[可以在响应中自行添加相关字段信息]

response.setHeader("sessionstatus","nologin");

return;

}

}

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request,IOException

{

this.doGet(request,response);

}

}

$.ajaxSetup({

contentType:"application/x-www-form-urlencoded;charset=utf-8",complete:function(XMLHttpRequest,textStatus){

//通过XMLHttpRequest取得响应头,sessionstatus,

var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus");

if(sessionstatus=="nologin"){

//如果超时就处理 ,指定要跳转的页面

window.location.replace("${path}/common/login.do");

}

}

}

});

总结

以上是编程之家为你收集整理的超时下Ajax请求处理全部内容,希望文章能够帮你解决超时下Ajax请求处理所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

Ajax请求session超时处理流程 java服务器端处理: SessionValidateFilter中修改: if (ServerInfo.isAjax(request)) { request.setAttribute("statusCode", 301); request.setAttribute("message", "Session timeout!"); response.sendRedirect(response.encodeRedirectURL("/ajaxDone.jsp"); else { response.sendRedirect(response.encodeRedirectURL(this.loginUrl + java.net.URLEncoder.encode(backToUrl, "UTF-8"))); } ajaxDone.jsp页面 <%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> { statusCode:${statusCode}, message:"${message}", objectId:"${objectId}" } js客户端处理ajax load页面碎片处理: 自己写一个loadUrl()方法,不能使用jquery自带的load(). 当客户端调用loadUrl()超时,弹出一个登录框,并加一个背景层下面的整个浏览器.这时浏览器窗口内容不能变,只是上面加了一个登录框和一个背景层 当用户输入username and password登录成功后,去掉登录框和背景层.这时用户可以继续操作. 登录失败alert出错信息,浏览器窗口内容还是不变. var DWZ = { loginUrl:"/render.do?method=login", ajaxDoneEval:function (json) { //session timeout try{ return eval('(' + json + ')'); } catch (e){ return {}; } } }; (function($){ $.extend({ loadUrl: function(url,data,callback){ var aData = data || {}; aData["timestamp"] = new Date().getTime(); var $this = $(this); $.get(url, aData, function(data){ var json = DWZ.ajaxDoneEval(data); if (json.statusCode==301){ alertMsg.error(json.message, {okCall:function(){ window.location = "/render.do?method=login"; //popLoginWin(); }}); } else { $this.html(data).initUI(); if (jQuery.isFunction(callback)) callback(); } }); } }); })(jQuery); ajax post 表单数据处理: 当客户端ajax提交表单超时, 弹出一个登录框,并加一个背景层下面的整个浏览器. 当用户输入username and password登录成功后,去掉登录框和背景层.这时用户可以继续操作. 登录失败alert出错信息. $.post(form.action, params, callback(json){ if (json.statusCode == 301) { //301 状态表示 session timeout popLoginWin(); }else if (json.statusCode == 300) { //300 状态表示 操作失败 alertMsg.error(json.message); } else if(json.statusCode == 200) { //300 状态表示 操作成功 alertMsg.correct(json.message); } }, "json");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值