0

I am trying to get data from a json file located in assets folder. But instead getting error.

heroes.service.ts

getPeopleData(): Observable<people[]>{
  return this.http.get<people[]>('assets/data/someData.json');
}

people.interface.ts

export interface people{
    id: number,
    name: string,
    age: number
}

someData.json:

[
    {"id":1, "name": "Jack", "age": 21},
    {"id":2, "name": "Rina", "age": 29},
    {"id":3, "name": "Jonathan", "age": 42}
]

about.component.ts

  peopleData:any;

  ngOnInit() {
    this.getPeople();
  }

  getPeople(): void {
   this.heroesService.getPeopleData()
    .subscribe(data => this.peopleData = data)
  }

Error:

enter image description here

Can someone help me out, where is the problem? There are no errors in console instead.

Deadpool
  • 7,811
  • 9
  • 44
  • 88

1 Answers1

1

This is working for me:

Service:

public testJson(): Observable<people[]>{
        return this.http.get("https://api.myjson.com/bins/1f5zag").map(res => res.json());
    }

(You can actually try it with that url)

component.onInit

this.yourService.testJson()
    .subscribe( evt => {
      const ppl:people[] = evt;
      console.log(ppl);
    });
Jacopo Sciampi
  • 2,990
  • 1
  • 20
  • 44