0

I am sending some data to my api by post and when it successfully submitted, it will return some data and I want to access the response data.

This is what I've got from my component :

this.http.post(this.restProvider.restApiUrl+'saveDraft', draftData, options)
.subscribe(data => {
   console.log(data["_body"]);
}, error => {
   console.log("Oooops!");
});

The console.log(data["_body"]); will resulting this data : {"status":"ok","data_id":"2","statusMsg":"Saved as draft"}

What I'm trying to do now is to access the value of data_id but I'm not really sure how to get it inside my component. I thought it can be accessed by something like data["_body"]["data_id"]

Emerald
  • 864
  • 1
  • 14
  • 37

2 Answers2

0

I think data is an object. Try this:

console.log(data._body.data_id);
console.log(data["_body"].data_id);
0

Finally, I solved the issue by changing the console.log(data["_body"]); to console.log(data.json().data_id);

I refer to this discussion Angular 2: How to access an HTTP response body? and tried to apply the JSON and it works.

Emerald
  • 864
  • 1
  • 14
  • 37