I am facing an issue within my app with this particular piece of code.
axios.post('url', {id:123});
window.location.href = window.location.pathname + '?lang=' + this.language;
The problem lies in the fact that the request is getting cancelled, possibly due to an immediate refresh taking place.
Here's what I attempted:
window.addEventListener('unload', (event) => {
axios.post('url', {id:123});
});
window.location.href = window.location.pathname + '?lang=' + this.language;
Unfortunately, this approach did not resolve the issue and the request still gets cancelled.
Is there a solution to ensure that the API request is sent before the page refreshes? I prefer not to use setTimeout as I cannot afford to wait more than 1 second for the refresh to occur.