1

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.

Gurbela
  • 1,184
  • 2
  • 16
  • 40
deepak murthy
  • 411
  • 3
  • 6
  • 21
  • Read this https://stackoverflow.com/questions/45827514/angular-4-convert-http-response-observable-to-object-observable – Gurbela May 28 '18 at 13:13

1 Answers1

4

Use this

import 'rxjs/add/operator/map';

this.http.get('YOUR_API_ENDPOINT').map(res => res.json()).subscribe(data => {
      console.log(data);
});
Parvej Mallick
  • 467
  • 1
  • 7
  • 11