【问题标题】:ionic 3 acces to json key离子 3 访问 json 密钥
【发布时间】:2018-02-22 14:20:29
【问题描述】:

我无法从 restful web 服务访问 json 响应的键。

{"_body":"{\"values\": {\"user_id\":\"1\",\"name\":\"fred test\",\"email\":\"fred@test.test\",\"username\":\"fredtest\",\"token\":\"d5f66a06ec809d70d0c52842df8dc0011d7d1ad0f2d56f50d3123da17a2489fe\"}}","status":200,"ok":true,"statusText":"OK","headers":{"pragma":["no-cache"],"content-type":["text/html;charset=UTF-8"],"cache-control":["no-store"," no-cache"," must-revalidate"],"expires":["Thu"," 19 Nov 1981 08:52:00 GMT"]},"type":2,"url":"http://localhost/PHP-Slim-Restful/api/login"}

我想访问此函数中的“值”:(this.responseData.values)

login(){
console.log('login'+ this.userData);
// Your app login API web service call triggers
this.authService.postData(this.userData,'login').then((result) => {
  this.responseData = result;

  console.log('userdata : '+ temp);
  if(this.responseData.values){
    console.log('response: ' +  this.responseData);
    localStorage.setItem('userData', JSON.stringify(this.responseData));
    this.navCtrl.push(TabsPage);
  }
  else{
    this.showToastWithCloseButton()
  }
}, (err) => {
  console.log('erreur : '+err);
});

}

我有一个未定义的错误!

你能帮帮我吗?

【问题讨论】:

    标签: angularjs json ionic-framework


    【解决方案1】:

    我使用 Observable 返回 json 数据并在我的方法中使用 subscribe 函数并使用 response.json() 从 RESTful webservices 转换 JSON 响应。

    我的组件方法,

    import {Http, Headers, Response, RequestOptions} from '@angular/http';
    import {Observable} from 'rxjs/Rx';
    
    var response = this.service.post('deleteUserDetails/'+this.selectedUserId, null);
       response.subscribe((res) => {
       var response = res.json();
    });
    

    服务发布方法,

    post(url: string, data : any): Observable<any> {
       let headers = new Headers();
       headers.append('Content-Type', 'application/json');
    
       let options = new RequestOptions({ headers: headers});
       return this.http.post(url, data,{headers: headers});
    }
    

    我认为这可能对您的查询有所帮助。

    【讨论】:

      【解决方案2】:

      您可以在 JSON 中创建一个 for 并访问帖子的返回值。类似的东西。

      "this.responseData = result.json();" -> 返回 JSON。为。

      示例:

      public postData(data, url: string) {
          this.http.post(url, data).toPromise().then(res => {
              let responseData = res.json();
              if (responseData) {
                  for (var item of responseData) {            
                      //Implments
                  }
              }
          }, (err) => {
      
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多