首页 > 解决方案 > Angular http 调用标头显示 200 响应,但响应数据显示 html 而不是 json 数据

问题描述

我正在使用 httpClient 对 api 进行 http 调用并在 ngfor 中显示项目。这似乎有效,因为我的列表中显示了三个项目,就像从我的 api 返回的三个项目一样。但是我似乎无法访问返回的 json 数据。当我在 chrome 的网络选项卡中查看网络调用时,我看到 200 成功,但响应是 html。我猜我需要在某处指定 json 数据类型,但我不确定。

这是我的服务中的 http 调用:

  getProducts(): Observable<Product[]> {
    return this.http.get<Product[]>(this.competitionUrl)

这是我尝试使用组件中的数据的方式:

productlist = this.productdataservice.getProducts()

在我的html中:

<ion-list *ngFor="let product of productlist | async; let i = index">
  <button ion-item>
    <ion-avatar item-start>
      <img src="../../assets/icon/favicon.png">
    </ion-avatar>
    <h2>{{product.productName}}</h2>
  </button>
</ion-list>

我的列表应该显示三个图标,因为我的 api 返回三个产品,但是当我从我的角度前端页面查看网络调用时,我没有看到 json 数据。

标签: javascriptangulartypescriptangular-httpclientangular9

解决方案


推荐阅读