Конвертировать Http-ответ на объект Json Ionic 3 - PullRequest
0 голосов
/ 28 мая 2018

Следующий ответ возвращается при вызове функции регистрации

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() {
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");
});
}

Когда я делаю console.log(JSON.stringify(this.responseData));, добавляются обратные слеши к объекту json

Как этого избежать и получить доступ к сообщению в ответе.

1 Ответ

0 голосов
/ 28 мая 2018

Используйте это

import 'rxjs/add/operator/map';

this.http.get('YOUR_API_ENDPOINT').map(res => res.json()).subscribe(data => {
      console.log(data);
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...