[英]Error while using body.json() for parsing response from http.get()
嘗試使用 body.json() 將數據分配給 object 數組,但當它返回 promise 時嘗試了此操作。 但是瀏覽器拋出錯誤,告訴我 json() 不是 function。
getRecipes() {
this.http.get('https://recipe-book-1be52.firebaseio.com/recipes.json').subscribe(
(response: Response) => {
response.json().then(
(data) => {
this.recServ.setRecipes(data)
}
);
}
)
}
angular httpClient 已經為你做了.json()
下面的代碼片段可以幫助你
getRecipes() {
this.http.get('https://recipe-book-1be52.firebaseio.com/recipes.json').subscribe(
(response: Response) => {
this.recServ.setRecipes(JSON.parse(JSON.stringify(response)));
}
)}
您實際上可以像這樣替換它,此外,可以將響應分配給接口以對其進行嚴格類型化。
getRecipes() {
this.http.get('https://recipe-book-1be52.firebaseio.com/recipes.json').subscribe(
(response) => this.recServ.setRecipes(response)
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.