繁体   English   中英

使用 body.json() 解析来自 http.get() 的响应时出错

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM