I am getting an error Preflight request for request with keepalive specified is currently not supported
on calling a POST request using the fetch api with keepalive
enabled. Any help would be appreciated . I am calling this api on beforeunload
event.
API Request
fetch(uri, {
method: 'POST',
headers: {
'Content-type': options.headers.get('content-type'),
'Authorization': options.headers.get('authorization')
},
body: JSON.stringify(interactionBody),
keepalive: true
}).catch((e) => {
console.log(e);
});
keepalive. The keepalive option can be used to allow the request to outlive the page. Fetch with the keepalive flag is a replacement for the Navigator.sendBeacon() API. signal. An AbortSignal object instance; allows you to communicate with a fetch request and abort it if desired via an AbortController .
The fetch() method in JavaScript is used to request data from a server. The request can be of any type of API that return the data in JSON or XML. The fetch() method requires one parameter, the URL to request, and returns a promise.
The simplest use of fetch() takes one argument — the path to the resource you want to fetch — and does not directly return the JSON response body but instead returns a promise that resolves with a Response object.
The serverless function returns the data back into your client application. Since the serverless function runs securely on the hosting providers servers and is responsible for the requests, the API key is never handled directly by the client, and is therefore secure.
I found a workaround until the Chrome issue fixed
it working when using mode: 'same-origin'
fetch(uri, {
method: 'POST',
headers: {
'Content-type': options.headers.get('content-type'),
'Authorization': options.headers.get('authorization')
},
body: JSON.stringify(interactionBody),
mode: 'same-origin',
keepalive: true
})
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