简体   繁体   中英

Navigate to login page when session expires with AJAX

I got a small issue when navigating back to the login page after the session expires. I have a link on my page to view the password using ajax, if the session does not expires, the "view password" link will be replaced by the password for a moment; otherwise, it will navigate to the login page. I use this javascript to check whether the seesion still exist or not

$(document).ready(function() { 

    $(document).bind("ajaxComplete", function(event, response, ajaxOptions) {
        if (response.getResponseHeader('SEESION_EXP') === '1') {
            self.location = _CONTEXT_PATH;
            return false;
        }

    });

});

The problem here is, the login page will be replaced into the password place for a moment before user is actually navigated to the login page. Is there anyway to solve this problem?

Just get a string that you may use it for validation at client side. For example, my link invokes a servlet which will return 'redirectLogin' as repsonse to client if session is expired. If reponseText equals 'redirectLogin', then use window.location.replace for navigating to the desired page

$('#id_of_link').click(function(event) {
            event.preventDefault();   

            $.get('ActionServlet', function(responseText) { 

            if(responseText=="redirectLogin")
                window.location.replace("desired url");  
        });
    });

请检查response.getResponseHeader('SEESION_EXP')是否为1,然后使用window.location = _CONTEXT_PATH;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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