Ionic 2:无法在视图中显示JSON响应

时间:2016-05-12 11:39:46

标签: json angular ionic2

我正在尝试显示JSON响应,但似乎无法显示任何内容:

响应:

enter image description here

woo.service.ts:

--raw-output / -r
  With this option, if the filter's result is a string then it will be written
  directly to standard  output rather  than  being formatted as a JSON string
  with quotes. This can be useful for making jq filters talk to non-JSON-based
  systems

woo.ts:

getProducts() : Observable<any> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this._http.get(this._url+'products?'+'consumer_key='+this._ck+'&consumer_secret='+this._cs, {
      headers : headers
    }).map(res => res.json().items);
}

最后是woo.html:

  ngOnInit() {
      this._wooService.getProducts()
      .subscribe(
          response => this.response = response,
          error => console.log(error));
          this.isLoading = false;
  }

任何人都知道为什么我无法使用* ngFor =“#obj of response”访问?

1 个答案:

答案 0 :(得分:2)

我认为你的问题在这里:

return this._http.get(this._url+'products?'+'consumer_key='+this._ck+'&consumer_secret='+this._cs, {
  headers : headers
}).map(res => res.json().products); // <-----

而不是:

return this._http.get(this._url+'products?'+'consumer_key='+this._ck+'&consumer_secret='+this._cs, {
  headers : headers
}).map(res => res.json().items);

根据您的响应有效负载。

小注意事项:您不需要为获取请求设置Content-Type标题。