使用Angular 5和Laravel 5.5消耗json

时间:2017-12-15 21:41:38

标签: php json angular

我有这个Api我不知道如何使用或访问数据数组 我试试这个,但我没有得到P的产品清单 我有这个API

{"data":[{"name":"product1","price":55,"created_at":"2017-11-29 00:00:00","updated_at":"2017-11-29 00:00:00"},{"name":"product2","price":250,"created_at":"2017-12-09 00:00:00","updated_at":"2017-12-09 00:00:00"}]}

My Angular Code

p:Product[];
  constructor(private http: Http) {
    this.url = 'http://127.0.0.1:8000/api/products';
  }

  getProducts() {
     this.http.get(this.url, {headers: this.prepareHeaders()}).map(res=>res.json())
     .subscribe( p => this.p = p);
     console.log(this.p.toString());
     }

1 个答案:

答案 0 :(得分:0)

getProducts() {
 this.http.get(this.url, {headers: 
 this.prepareHeaders()}).map(res=>res.json())
 .subscribe( p => {this.p = p; console.log(p);});
 }

以下是带有Observable&的Http.get的一个很好的例子。保证Example