问题
Trying to fetch data from a json file in ionic 2 application, I am using the below piece of code within constructor for fetching json from assets folder, but getting error "Supplied parameters do not match any signature of call target." What i am missing, Please suggest.
this.http.get('assets/sample.json')
.map((res) => res.json().records)
.subscribe(data => {
this.data = data;
console.log("json data"+ data);
}, (rej) => {console.error("Could not load local data",rej)});
回答1:
So I solved it, the path of json file from assets folder used as below help me to get the answer that displayed the data on device.
this.http.get('/android_asset/www/assets/data/subjects.json').map(res => res.json()).subscribe(
response => {
this.posts = response;
console.log(this.posts);
},
err => {
console.log("Oops!");
});
While if we use /assets/data/subjects.json
, this will display the data on browser, though we can not use a single path that could be able to get the data on both browser as well as on device, because both have the different file system so we need different way to access the data on the devices.
回答2:
this.http.get("assets/sample.json")
.subscribe(res =>{
this.data=res.json();
this.data=Array.of(this.data);
console.log(this.data);
}, error =>{
console.log(error);
});
Try this.
来源:https://stackoverflow.com/questions/46176673/fetching-data-from-json-file-in-assets-folder