Getting local JSON data


#1

Hello, I am trying to load a local JSON file located in the assets folder.

 this.http.get('assets/data.json', {}, {})
      .then(data => {
        let returnJson = JSON.parse(data.data);
        this.items = returnJson;
      }).catch((error) => {
        this.error_msg = error.error;
      });

It keeps triggering the catch: There was an error with the request

Thanks!


Ion-select
#2

I think you need ionic file plugin to access the contents of file placed in assets folder.

this.file.readAsText(this.file.applicationDirectory + "www/assets", "data.json").then(...)

This should work.


HTTP request for local JSON file
#3

This my code for getting json data from a file in a sub folder of assets called data and the file named subtitles.json

this.http.get('assets/data/subtitles.json').map(res => res.json()).subscribe(data => {
               this.subtitles = data;
          });

#4

How can i read local json file with the ionic native HTTP plugin(not angular http)?


#5

why do you want to use the native ionic http plugin? That plugin might not have access to the internal files.


#6

Because I have no access to backend and I can not enable cors on it, because of that, the new webkview on ios not pass http request across domains with the angular http client