I have been struggling to write code that will make a POST request reliably on close of the tab window. Navigator.sendBeacon
seems to be exactly what I need (I only require this to work for Google Chrome).
$(global).bind('unload', function () {
let body = {
UserEmail: appState.user.email,
Job: {
Id: appState.jobId
},
Timestamp: '/Date(' + new Date().getTime() + ')/',
EventOrigin: 'PdfReviewClient',
Event: 'JobClosed'
};
let headers = {
Authorization: `JWT ${authenticationState.token}`,
'Content-Type': 'application/json; charset=utf8'
};
let blob = new Blob([JSON.stringify(body)], headers);
navigator.sendBeacon(configuration.rootApiUrl + 'jobevents', blob);
});
My beacon includes custom headers, that's why I create a Blob.
However, this request does not seem to be happening. This is especially hard to debug since the window closes. So the question is, why is my beacon not sending?