@Vipul Panth has useful information, but I would like to provide more detailed information.
First, note that navigator.sendBeacon not supported in all browsers. See the MDN documentation for more information on this feature and the browsers currently supported.
You really create a blob to provide headers. Here is an example:
window.onunload = function () { let body = { id, email }; let headers = { type: 'application/json' }; let blob = new Blob([JSON.stringify(body)], headers); navigator.sendBeacon('url', blob); });
navigator.sendBeacon will send a POST request with the Content-Type request header set to everything that is in headers.type . This seems to be the only heading you can set in the beacon, though, beyond the W3C :
The sendBeacon method does not provide the ability to configure the request method, provide its own request headers, or change other properties of request and response processing. Applications that require custom settings for such requests should use the [FETCH] API with the keepalive flag set to true.
I was able to observe some of how this worked through this Chromium bug report .
source share