0

I'm trying to make this post call when user closes the window.

createCallRecord (data) {
        let logData = {"chathist" : data , "uid": "1111" };
        let headers = new HttpHeaders();
        headers = headers.set("Content-Type", "application/json");
        let options = { headers: headers };
        return this.http.post(MY_URL, logData, options).subscribe(res => alert(res));
}

Since this is called when the user closes the window, i don't know whether it's posting sucessfully or not.

How to handle success or error after posting?

For now, it is not alerting anything.

Any help would be appreciated!

Ramya S
  • 2,954
  • 5
  • 14
  • 24
  • https://stackoverflow.com/questions/44385777/how-does-http-error-handling-work-with-observables#44386436 – CozyAzure Apr 13 '18 at 07:28

1 Answers1

0

You can handle the error in this way:

createCallRecord (data) {
    let logData = {"chathist" : data , "uid": "1111" };
    let headers = new HttpHeaders();
    headers = headers.set("Content-Type", "application/json");
    let options = { headers: headers };
    return this.http.post(MY_URL, logData, options)
        .subscribe(result =>
        {
          let value = result.json();
          console.log(JSON.stringify(value));
        }, err => { console.log(err); });

Another option is to use handler funtion in this way:

createCallRecord (data) {
    let logData = {"chathist" : data , "uid": "1111" };
    let headers = new HttpHeaders();
    headers = headers.set("Content-Type", "application/json");
    let options = { headers: headers };
    return this.http.post(MY_URL, logData, options)
        .subscribe(res => this.resultHadler(res), err => this.errorHandler(err));
}

private resultHadler(res: any)
{
    let value = res.json();
    console.log(JSON.stringify(value));
}

private errorHandler(err: any)
{
    console.log(err);
}
Marco Barbero
  • 1,460
  • 1
  • 12
  • 22