Using ajax asynchronous request, the springMvc interceptor jump page is invalid (solve the Ajax request backend response.sendRedirect() failure)

					本文解决Ajax请求后端 response.sendRedirect()失效

The author is making a requirement, and needs to intercept users who visit the website but have not logged in, and redirect them to the home page. It is a very simple requirement, which can be fulfilled by using the interceptor of springMvc; but when coding, the page cannot always be redirected. Later, I searched on Google and found out the cause of the problem. The front end and the front end were separated, and the foreground used ajax asynchronous request;

Ajax, what is ajax, simply put, it is a partial web page refresh technology. After the front-end and back-end are separated, the background gradually weakens the control of page logic jumps, and these are handed over to the front-end. The front-end uses ajax to request the background asynchronously, obtains data, and partially refreshes the webpage, realizing many convenient functions;

Therefore, even if the code for page jump is performed in the background, and after the front-end request is completed, only the ajax callback function will be executed. As for the logic in the background, I’m sorry, I don’t care about ajax at all; after hearing this, everyone should also guess what to do Yes, yes, start throwing the pot away, and say loudly to the front-end classmates, this is your business, you perform page jumps in the ajax callback function, how fat is your front-end technology.

$.ajaxSetup

Don’t worry before changing ajax. If you make changes in the callback function mentioned above, then the code of the front-end students will have to be changed a lot. Any method involving ajax request must add the logic of page jump. . .

Using $.ajaxSetup( ) here can be seen as an enhancement of ajax. Compared with the post-enhancement of spring transactions, $.ajaxSetup( ) will be executed after each ajax request is completed (does it feel like a post-enhancement ah);

Give the following code to the front-end children's shoes, and let him put it in the imported public js file.

// 解决Ajax异步请求 springMvc 不跳转页面的问题
$.ajaxSetup( {
    
    
	//设置ajax请求结束后的执行动作
    complete :
        function(XMLHttpRequest, textStatus) {
    
    
			// 通过XMLHttpRequest取得响应头,sessionstatus
            var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
            if (sessionstatus == "TIMEOUT") {
    
    
                var win = window;
                while (win != win.top){
    
    
                    win = win.top;
                }
                win.location.href= XMLHttpRequest.getResponseHeader("CONTEXTPATH");
            }
        }
});

The meaning of this code is roughly that, after the ajax request is completed, the complete callback method in $.ajaxSetup is executed, and some logical judgments are made in it; here, the state of the
session is judged, and if the timeout is destroyed, the page is jumped Turn, jump to the value of CONTEXTPATH; the state of the session and the value of CONTEXTPATH ​​are defined by our background students;

As follows: In the preHandle method of the interceptor, add the following logic

HttpSession session = httpServletRequest.getSession();
     User user = (User) session.getAttribute("user");
     if (user == null) {
    
    
         // 获取到项目名,以便下面进行重定向
         String homeUrl = httpServletRequest.getContextPath();
      	// 如果是 ajax 请求,则设置 session 状态 、CONTEXTPATH 的路径值
      	// 如果是ajax请求响应头会有,x-requested-with
         if (httpServletRequest.getHeader("x-requested-with") != null && httpServletRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
    
    
             httpServletResponse.setHeader("SESSIONSTATUS", "TIMEOUT");
             httpServletResponse.setHeader("CONTEXTPATH", homeUrl+"/index.html");
             // FORBIDDEN,forbidden。也就是禁止、403
             httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); 
         }else{
    
    
             // 如果不是 ajax 请求,则直接跳转即可
             httpServletResponse.sendRedirect(homeUrl+"/index.html");
         }
         return false;
     }

Because of the above processing, springMvc can continue to happily handle page jumps again;

The main thing is to remember the ajax asynchronous request. The logic of the foreground will use the ajax callback function in the end, and it will only accept the returned data from the background, and will not respond to the request of page jump, because after all, it is only a partial refresh, but the background needs the entire The page refreshes. .

Guess you like

Origin blog.csdn.net/weixin_44004020/article/details/110650358