I am trying to send a POST HTTP request before browser close.
Basically I'm trying to event out the user action that they've closed the page, but the call isn't making it out of the app before it shuts down.
I am under the impression that this isn't possible with a normal HTTP request because the fact you must subscribe to the HTTP request.
I've tried the following solution found here: Angular 2 - Execute code when closing window
This won't work for me as I'm not doing simple GET. Here's what I've got so far.
component.ts
@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler($event) {
this.eventsService.sendEvent(this.form.value).subscribe(() => {
console.log("Made it");
});
}
service.ts
sendEvent(userData): Observable<any> {
return this.http.post<any>(this.url, userData,
{
headers: new HttpHeaders({
"Content-Type": "application/json"
})
})
}
Is there a way to utilize XMLHttpRequest() like the solution above states? What other options are there for a solution like this?
Any help is appreciated.