微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

超时下Ajax请求处理

public class UserLogin extends HttpServlet
{
    
    /** serialVersionUID */
    private static final long serialVersionUID = 464654165487455L;
    
    /**
     * 这里只是示例代码,没有考虑代码的合理性:
     * 
     * The doGet method of the servlet. <br>
     * 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. <br>
    *
    * 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");    
                                }    
                     }    
            }    
           });  

原文地址:https://www.jb51.cc/ajax/163863.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐