Jan van Bergen
Jan van Bergen

Reputation: 111

.json file not recognised using http.get?

I'm trying to access input data that's stored inside of a .json file but for some reason my path isn't recognised:

This method runs when data is requested:

    getData() {
  console.log('this is working');
  this.http.get('src/app/inputs/inputs.json')
  .subscribe(
    (data: any) => {
      console.log('this is working');
      console.log(data.text());
     }
   );
  }
}

This is my folder structure:

enter image description here

I am able to add the data whenever a separate button is clicked, just retrieving it is where I get stuck.

Upvotes: 0

Views: 293

Answers (2)

Piero
Piero

Reputation: 1726

I think you have to add your inputs folder in the assets section of angular.json eg

"assets": [
  "src/assets",
  "src/inputs",
  "src/favicon.ico"
]

then access your data through

this.http.get('inputs/inputs.json')

Upvotes: 3

Saksham Gupta
Saksham Gupta

Reputation: 628

Try this.http.get('./inputs/inputs.json')

Upvotes: 0

Related Questions