HTTP request for local JSON file

Hi all.

I have been struggling with this for a very long time and am completely stumped. I am using Ionic 3.

I am trying to load a local JSON file from assets/data/oral/oral-template.json. It works absolutely fine using the Angular Docs guide for Local JSON data on the browser simulator, but when building on device it does not work at all.

I also attempted to use Ionic File to do the same task but the documentation for that was absolutely opaque and didn’t give any of the information I could use to read this local file.

   setupQuestionService() {
    this.http.get('/assets/data/oral/oral-template.json')
    .subscribe(res => {  
      console.log("HTTP RESPONSE:")
      console.log(res)
      let data = (res as any).questions 

      for (let dataIndex=0; dataIndex<data.length; dataIndex++) { 
        let newQuestion = new OralQuestion(data[dataIndex].module, data[dataIndex].section, data[dataIndex].questionText, data[dataIndex].answerText)
        this.questions.push(newQuestion)
      }
    })
  }

Again, this works absolutely fine on the ionic serve -l simulator but when building on my iPhone it doesn’t respond.

Thanks for any help you can give.

Maybe I was just being stupid before, but I managed to get it working with Ionic File. Thanks to akshukla07 for giving the only source I could find that explained usage.

If anyone has a source that explains how Ionic File works with Android and iOS app filesystems I would hugely appreciate it!

 setupQuestionService() {
    this.file.readAsText(this.file.applicationDirectory + "www/assets/exams/oral", "oral-template.json")
    .then(res => {
      let data = (JSON.parse(res) as any).questions 
      for (let dataIndex=0; dataIndex<data.length; dataIndex++) { 
        let newQuestion = new OralQuestion(data[dataIndex].module, data[dataIndex].section, data[dataIndex].questionText, data[dataIndex].answerText)
        this.questions.push(newQuestion)
      }
    })
  }
1 Like

Are the assets included in the build?
I have no experience with ios builds

It is supposed to work and less tedious compared to File, imho