超时下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");    
                                }    
                     }    
            }    
           });  

相关文章

JS原生Ajax操作(XMLHttpRequest) GET请求 POST请求 兼容性问题 利用iframe模拟ajax 实现表单提交的返回...
AJAX 每日更新前端基础,如果觉得不错,点个star吧 &#128515; https://github.com/WindrunnerMax/E...
踩坑Axios提交form表单几种格式 前后端分离的开发前后端, 前端使用的vue,后端的安全模块使用的SpringSe...
很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈哈。 为了防止...
需要在服务器上进行哈 jquery的ajax方法: // jquery请求 $.ajax({ url: &quot;./server/slider.js...
Ajax函数封装ajax.js // Get / Post // 参数 get post // 是否异步 // 如何处理响应数据 // URL // var...