【问题标题】:Convert Http Response to Json object Ionic 3将 Http 响应转换为 Json 对象 Ionic 3
【发布时间】:2018-05-29 01:24:20
【问题描述】:

调用注册函数时返回以下响应

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 对象中

如何避免这种情况并在响应中访问消息。

【问题讨论】:

标签: ionic-framework


【解决方案1】:

使用这个

import 'rxjs/add/operator/map';

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2017-08-20
    • 2017-09-13
    • 2018-07-26
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多