Hello,
Update:
So I understood that it's not possible to do that, I will then need to send the CSRF token in the data, and extract it in the backend. But it means I have to pass Laravel's CSRF check and do it myself.
What is a good way to do that then? i.e. to validate the token myself and prevent Laravel from checking it thus throwing me 419?
---- Original Post ----
I need to send the CSRF token in the header otherwise I get 419 error.
I found this post: https://stackoverflow.com/questions/40523469/navigator-sendbeacon-to-pass-header-information
But it's not helpful, the answers there either say to use fetch, or they say you can send headers, but then they only show:
const headers = {
type: 'application/json',
};
That's only the type, what about the data itself which is the CSRF token:
let csrfToken = $('meta[name="csrf-token"]').attr('content')
Is it still not possible to send it via sendBeacon and I will need to use fetch as suggested with the keepalive flag, or, it can be done?
The following doesn't work either:
const url = "/some/url",
data = new FormData(),
token = $('meta[name="csrf-token"]').attr('content');
data.append("foo", "bar");
data.append("X-CSRF-TOKEN", token);
navigator.sendBeacon(url, data);