Use beforeunload
event. Normally, if there are asynchronous actions in that handler, browser won't wait for them to finish and will proceed with page unload (effectively interrupting your ajax call before it reaches your server).
There are some (hackish) tricks to make sure your ajax gets through before browser will unload page. One of those (used for example by services that track your behavior on webpages) is doing a loop, like so:
window.addEventListener('beforeunload', function() {
// number of miliseconds to hold before unloading page
var x = 200;
var a = (new Date()).getTime() + x;
// -----------
// your ajax call goes here
// -----------
// browser will hold with unloading your page for X miliseconds, letting
// your ajax call to finish
while ((new Date()).getTime() < a) {}
}, false)