【问题标题】:Print JSON response from REST api in ionic在 ionic 中打印来自 REST api 的 JSON 响应
【发布时间】:2019-01-02 13:14:40
【问题描述】:

我正在尝试打印从 RESTful API 请求中获得的 JSON 响应:

products:Observable<any>;
constructor(public navCtrl: NavController, private backgroundGeolocation: BackgroundGeolocation, public zone: NgZone, private auth: AuthService, public httpClient: HttpClient)
{

    this.products = this.httpClient.get('http://127.0.0.1:8000/product');
}

如果我在控制台中打印结果,它确实可以正常工作:

this.products
      .subscribe(data => {
        console.log('my data: ', data);
      });

数据是对的。

但是现在,我不知道如何将它们打印到 HTML 页面上。我试过了,但它不起作用:

<ion-list>
        <ion-item *ngFor="let p of (products | async)?.results">{{ p.productName}}
        </ion-item>
</ion-list>

还有其他方法可以解决问题吗?

我的 JSON 响应是这样的:

0: Object { idProduct: "1", productName: "Pasta", purchased: "0" }
​
1: Object { idProduct: "2", productName: "latte", purchased: "0" }

我已经解决了这个问题。我想发布解决方案以帮助其他用户在这种糟糕的情况下。 解决方案就是这么简单。我创建了一个名为“rest-service”的新打字稿文件,由:

@Injectable()
export class RestServiceProvider {

  constructor(public http: HttpClient) {
    console.log('Hello RestServiceProvider Provider');
  }
  getUsers() {
    return new Promise(resolve => {
      this.http.get('http://127.0.0.1:8000/product').subscribe(data => {
        resolve(data);
      }, err => {
        console.log(err);
      });
    });
  }
}

现在,在 home.ts 中我已经做到了:

getUsers() {
    this.restProvider.getUsers()
      .then(data => {
        this.products = data;
        console.log(this.products);
      });
  }

然后,在构造函数中:

this.getUsers();

在HTML端,解决方法非常非常简单:

<ion-item *ngFor="let p of products"> {{ p.productName }}

不过,谢谢大家

【问题讨论】:

    标签: html json rest ionic-framework


    【解决方案1】:

    请尝试将产品转换为数组对象。这可能会解决您的问题。

    this.products = this.products.json();
    

    【讨论】:

    • 对不起,谢谢您的回复,但是,什么是:产品? products 对象没有任何 json() 方法
    • 对不起,我的意思是this.products
    • 反正没有json方法。我正在尝试这样做:
    • this.products .subscribe((data: Response) => { this.products = data.json(); });
    【解决方案2】:

    更新

    看来您找到了解决方案。通常我不喜欢直接在函数中使用*ngFor

    <!-- DON'T DO THIS -->
    <ion-item *ngFor="let item of someFunction()">
    

    而是在构造函数之上定义一个变量,然后赋值数据。不需要单独的文件,但如果您一遍又一遍地执行相同的请求,它会很有用。

    TypeScript

    items: Array<any> = [];
    
    constructor() {
      // ...
    }
    
    ionViewDidLoad() {
      this.http.get('https://someurl.com/data').subscribe((data: any) => {
        this.items = data;
      }).catch(err => console.error('Something went wrong: ', err));
    }
    

    HTML

    <ion-item *ngFor="let item of items">
        {{ item.name }}
    </ion-item>
    

    旧答案

    如果你只是跟随会发生什么?

    <ion-item *ngFor="let p of products">
    

    您尝试访问 products-array 上的 .results 的任何原因?

    如果您在控制台中遇到任何错误,请与我们分享。

    【讨论】:

    • 错误:未捕获(在承诺中):错误:找不到“object”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
    • 这是错误。我也尝试过这样的静态方式: this.samples = [ {"date" : "Thursday 16 Mar", "Data1" : "Sample Child","Data2" : "Sample Class"}, {"date" : "3 月 14 日星期二", "Data1" : "Sample Child","Data2" : "Sample Class"}, {"date" : "Friday 10 Mar","Data1" : "Sample Child","Data2" : "Sample Class"}] 而且,如果我这样做 它可以工作。好奇怪
    • 我认为可能是 JSON 响应中没有父级。在其他帖子上,我找到了这个解决方案: {{ p.attribute }} 但是这里没有父项
    猜你喜欢
    • 2021-05-27
    • 2022-01-13
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    相关资源
    最近更新 更多