deepak murthy
deepak murthy

Reputation: 411

Convert Http Response to Json object Ionic 3

The below response is returned upon calling the signup function

Response {_body: "string(85) "{"message":"A customer with the same email 
already exists in an associated website."}"↵", status: 200, ok: true, 
statusText: "OK", headers: Headers, …}

headers: Headers {_headers: Map(1), _normalizedNames: Map(1)}
ok: true
status: 200
statusText: "OK"
type: 2
url: "http://127.0.0.1/sandbox/M2API/signup/signup"
_body: "string(85) "{"message":"A customer with the same email already exists in an associated website."}"↵"
__proto__: Body

Signup Function:

signup() {
this.authServiceProvider.postData(this.userData, "signup").then((result) => {
  this.responseData = result;
  console.log(this.responseData);
  if( (JSON.stringify(this.responseData._body)) != "" ) {
    this.navCtrl.setRoot(HomePage);
  } else {
    console.log("User already exists");
  }
}, (err) => {
  //connection failed error message
  console.log("something went wrong");
});
}

When i do console.log(JSON.stringify(this.responseData)); backslahes are added to json object

How to avoid that and access message in the response.

Upvotes: 1

Views: 1683

Answers (1)

Parvej Mallick
Parvej Mallick

Reputation: 465

Use this

import 'rxjs/add/operator/map';

this.http.get('YOUR_API_ENDPOINT').map(res => res.json()).subscribe(data => {
      console.log(data);
});

Upvotes: 4

Related Questions