Ionic 2:无法将 JSON 响应显示到视图中
Ionic 2 : Cannot display JSON response into view
我正在尝试显示 JSON 响应,但似乎无法显示任何内容:
回复:
woo.service.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.ts:
ngOnInit() {
this._wooService.getProducts()
.subscribe(
response => this.response = response,
error => console.log(error));
this.isLoading = false;
}
最后 woo.html:
<ion-card-header class="cardHead" text-center>
<h2 class="cardHeaderText">{{obj.products.title}}</h2>
<p>Prix: {{obj.price_html}}</p>
</ion-card-header>
<ion-card-content>
<img src="{{obj.images.src}}">
<p class="cardBodyText">{{obj.description}}</p>
<p>Tags: {{obj.tags}}</p>
<p>Catégories: {{obj.categories}}</p>
</ion-card-content>
有人知道为什么我无法使用 *ngFor="#obj of response" 访问吗?
我认为你的问题出在这里:
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
header。
我正在尝试显示 JSON 响应,但似乎无法显示任何内容:
回复:
woo.service.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.ts:
ngOnInit() {
this._wooService.getProducts()
.subscribe(
response => this.response = response,
error => console.log(error));
this.isLoading = false;
}
最后 woo.html:
<ion-card-header class="cardHead" text-center>
<h2 class="cardHeaderText">{{obj.products.title}}</h2>
<p>Prix: {{obj.price_html}}</p>
</ion-card-header>
<ion-card-content>
<img src="{{obj.images.src}}">
<p class="cardBodyText">{{obj.description}}</p>
<p>Tags: {{obj.tags}}</p>
<p>Catégories: {{obj.categories}}</p>
</ion-card-content>
有人知道为什么我无法使用 *ngFor="#obj of response" 访问吗?
我认为你的问题出在这里:
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
header。