简体   繁体   English

window.location不适用于ajax调用

[英]window.location is not working with ajax call

I redirect to the main page during 2 scenarios 我在2种情况下重定向到主页

  1. While the user hits logout 当用户点击注销时
  2. The session expires... 会话到期...

Logout functionality code... 注销功能代码...

 $("#logoutLink").click(Logout);
    function Logout() {
      $.post("Logout",
           { antiCSRF: '{{acsrf}}',
             session_id: '{{session_id}}'
        });
      alert("You are now securely logged out.");
      window.location = './';
    }

Session expiration code 会话过期代码

function checksession() {

            $.post("CheckSession",
                     { antiCSRF : '{{acsrf}}',
                       session_id: '{{session_id}}'
                  },validateresult).fail(function() {alert("Session check failed")})

}



 function validateresult(result){
              if (result == "N"){
                valid_flag = "N"
                alert("Your session has been timed-out. Please click OK to be redirected to the login page to start a new session.");

                window.location = './';
              }
              else { valid_flag = "Y";
              return valid_flag
                }
            }

In the first case ie; 在第一种情况下,即; when I hit the logout button the page is redirected to the main page without any issues... 当我点击注销按钮时,页面被重定向到主页,没有任何问题...

But the code that gets executed during the ajax call does not work..ie; 但是在ajax调用期间执行的代码不起作用。 window.location is not working.. window.location不起作用..

Not sure what is the difference between the 不知道两者之间有什么区别

The ajax call is asynchronous. ajax调用是异步的。 You should show the alert() and perform the redirection in the callback function which is executed once the $.post() finishes: 您应该显示alert()并在$.post()完成后执行的回调函数中执行重定向:

$("#logoutLink").click(Logout);


function Logout() {
    $.post("Logout", {antiCSRF: '{{acsrf}}', session_id: '{{session_id}}'}, function() {
      alert("You are now securely logged out.");
      window.location = './';
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM