I'm trying to create a feature that when a user logs off, they are presented with a confirmation window if they have items in their cart. Below is my code snippet. I'm having trouble with the redirect and reload. I expect it to take me to the localhost:8080 login page, but it stays on whatever page I happen to be on. I know the logoff part is running b/c when I manually refresh the page after the below snippet runs, it changes to a guest user.
EDIT: I looked at the possible duplicate and it made the same suggestion as you guys have so far. Unfortunately it doesn't work. It leaves me on the same page as if nothing happened.
EDIT: It works on IE, but not on chrome and firefox. Updated code snippet
Modified Code Snippet:
$("#menu_sign_out").click(function(){
var result;
var httpRequest = new XMLHttpRequest(); //Asynchronous
httpRequest.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200){
result = this.responseText;
console.log(result);
myObj = JSON.parse(this.responseText);
if(myObj.count > 0){
if(confirm("You have items in your cart. Are you sure you want to leave?")){
var request = new XMLHttpRequest(); //Synchronous
request.open('GET', "/logout", false);
request.send(null);
window.location.replace('/login');
//window.location.href='/login';
} else {
//do nothing
}
} else {
var request = new XMLHttpRequest(); //Synchronous
request.open('GET', "/logout", false);
request.send(null);
window.location.replace('/login');
//window.location.href='/login';
}
}
};
httpRequest.open('GET', "/api/cart/countItems");
httpRequest.send();
});