Search code examples
angularangular2-http

angular 2 how to get returned data from .post http angular2


I am using HTTP angular2 to post some data. It is working ok, I can send data from angular2 service , receive it in my express backend.

I want to get the returned data from express. If i use .get .map((res:Response) => res.json()) works, but it is not working with .post.

This is not working::

 let successData = this._http.post(`${this.baseExpressUrl}adduser`, JSON.stringify(data), {headers:headers}).map((res:Response) => res.json());

Please help how can I subscribe in .post angular2.


Solution

  • There is no way to get the data outside of a callback. Move the code that depends on the response inside the callback passed to subscribe

    this._http.post(`${this.baseExpressUrl}adduser`, JSON.stringify(data), {headers:headers})
    .map((res:Response) => res.json())
    .subscribe((data) => console.log(data));