I'm trying to do this:
$(window).unload( function () {
$.ajax({
type: "POST",
url: "http://localhost:8888/test.php?",
data: "test",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
alert (c);
});
However, the success alert is never shown, nor does this request seem to be even hitting the server. What am I doing wrong?
I believe you need to make the request synchronous instead (it's asynchronous by default) using the async : false
parameter.
Synchronous requests lock up the browser until they complete. If the request is asynchronous, the page just keeps on unloading. It's quick enough that the request never even has time to fire off.
Try calling it with async = false;
jQuery.ajax({url:"http://localhost:8888/test.php?", async:false})
I just tried it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With