[英]Fetching data from json file via service is giving “unexpected end of input” in Angular4
I am trying to get data from a json file located in assets folder. 我正在尝试从资产文件夹中的json文件获取数据。 But instead getting error.
但是反而会出错。
heroes.service.ts heroes.service.ts
getPeopleData(): Observable<people[]>{
return this.http.get<people[]>('assets/data/someData.json');
}
people.interface.ts people.interface.ts
export interface people{
id: number,
name: string,
age: number
}
someData.json: someData.json:
[
{"id":1, "name": "Jack", "age": 21},
{"id":2, "name": "Rina", "age": 29},
{"id":3, "name": "Jonathan", "age": 42}
]
about.component.ts about.component.ts
peopleData:any;
ngOnInit() {
this.getPeople();
}
getPeople(): void {
this.heroesService.getPeopleData()
.subscribe(data => this.peopleData = data)
}
Error: 错误:
Can someone help me out, where is the problem? 有人可以帮助我,问题出在哪里? There are no errors in console instead.
控制台中没有错误。
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 component.onInit
this.yourService.testJson()
.subscribe( evt => {
const ppl:people[] = evt;
console.log(ppl);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.