So I’m using CodeIgniter framework to logout the user with the javascript below:
//If logged in:
$('#logout').click(function() {
$.ajax({url:'/auth/logout',
success: function(data){
$('#navigation').html(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
document.write(errorThrown);
}
});
location.reload(true);
});
As long as I comment out location.reload(true);, this code successfully calls the auth/logout function and logs the user out, which can be seen when I manually refresh the page. However, with the reload active the cookie doesn’t get deleted and the user stays logged in. How can I make sure the cookie is deleted before reloading the page?
You need to place the reload inside the success function like this:
You are reloading the page synchronically while the request is asynchronous, meaning the reload is happening before the AJAX is complete.