My problem is that when i call a function who listen to the event onBeforeUnload(), i want to post a data with a httpClient request. The problem is that my request is not sent. Here the code
@HostListener('window:beforeunload', ['$event'])
onBeforeUnload(): void {
this._httpClient.post(`${localhost:8080/apiRest}`, infoIWantToSent).subscribe();
}
I don't know if this is the good way to follow. Thank you in advance for your answer.
Why don't you use ngOnDestroy?
export class NewComponent implements OnDestroy{
ngOnDestroy() {
this._httpClient.post(${localhost:8080/apiRest}, infoIWantToSent).subscribe();
}
}
Thank you for your answer. the OnDestroy does'nt work in my case (never call). I find a solution by using :
@HostListener('window:beforeunload', ['$event'])
onBeforeUnload(): void {
const xhr = new XMLHttpRequest();
xhr.open('POST',${localhost:8080/apiRest, false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
return xhr.send(JSON.stringify(infoIWantToSent));
}
It's seems work. the token is the connexion token of my application.
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