user434885
user434885

Reputation: 2018

jquery ajax synchronous http request

i am using ajax and jquery to send an *synchonou*s http request to a php script in a web aplication to log out the user if incase the user decides to leave the page rather than logout.

i am using hte following code:

function close()
{
  return "If you exit you will be logged out ...!";
}
window.onbeforeunload = close;

$(window).unload( function () { 

$.ajax({
        url: "logout.php",
        async:false,
        success: function(msg){
        alert( "You have been logged out ...!");}
});     
});

the ajax is working in ALL cases apart from when i press the back button. When i press the back button i even get the success function running but when i check the php script has not run as i have a fwrite function in it to tell me it worked. it is working in other cases apart from the back button.

Upvotes: 0

Views: 1344

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075457

i am using ajax and jquery to send an asynchonous http request

The request in the code you've quoted is synchronous, not asynchronous.

If you're seeing the success function get called when you're not seeing the PHP code running, that means your browser has cached the previous result and reused it. You may be able to bypass that by adding cache: false to your request options.

Upvotes: 2

Related Questions