Is there a way to send a post request from my client to the server when the user closes the tab or browser window? I have tried using the 'windows.unload'or 'windows.beforeunload' event, but the call doesn't seem to be successful as the window closes before receiving a response. Here is the code snippet I have tried:
This is the 'onbeforeUnload' event:
window.onbeforeunload = ()=>{
this.store.dispatch<any>(this.mystore.UnLock());
}
This is the function in my Store:
UnLock() {
return (dispatch, getState) => {
dispatch(this.resetTimer());
return this.dataService.Locks('/unlock').subscribe(
(data: any) => {},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
dispatch(this.errormsg('Client-side error occured.'));
console.log('Client-side error occured.');
}
}
);
};
}
Here is my data service function:
Locks(url: string): Observable<any> {
return this.httpclient.post(url, {
headers: _headers
});
}
The issue I am facing is that my browser window closes before getting a response from the server. How can I resolve this problem?