fetching data from json file in assets folder

醉酒当歌 提交于 2019-12-24 11:06:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!