从angular2中的api接收json数组
我试图从api接收json数组并在页面上显示它,我很确定我的服务有问题,我只是不知道是什么从angular2中的api接收json数组,json,api,angular,Json,Api,Angular,我试图从api接收json数组并在页面上显示它,我很确定我的服务有问题,我只是不知道是什么 getAuctions(): Promise<Auction[]> { return this.http.get(this.auctionsUrl) .toPromise() .then(response => response.json().array as Auction[]) .catch
getAuctions(): Promise<Auction[]> {
return this.http.get(this.auctionsUrl)
.toPromise()
.then(response => response.json().array as Auction[])
.catch(this.handleError);
}
拍卖服务:
getAuctions() : Observable<Auction[]> {
return this.http.get(this.auctionsUrl)
.map((response: Response) => response.json())
.catch(this.handleError)
}
我在这里只是猜测,但我怀疑
response.json().array
可能是错误的,您需要检查哪些数据来了。它可能像删除.array
一样简单,但不知道数据的外观很难判断……我认为在您的情况下,response.json()
就足以获取数据
getAuctions() : Observable<Auction[]> {
return this.http.get(this.auctionsUrl)
.map((response: Response) => response.json())
.catch(this.handleError)
}
getAuctions(): void {
this.auctionService.getAuctions()
.subscribe(auctions => this.auctions = auctions);
}