I am currently experiencing an issue with my code. It works fine when fetching data from a URL, but when I try to read from a local JSON file located in the assets folder, it returns an error.
searchData() {
const url: any = 'https://jsonplaceholder.typicode.com/photos?albumId=1';
this.http.get(url).subscribe((res) => {
this.data = res;
console.log('Response Returned');
},
err => {
console.log('Error Response');
});
Output: Response Returned Expected: Response Returned
To fetch data from a local JSON file, I modified the path in the code to point to the local file within the assets folder. However, this change resulted in the code going to the error part and returning an Error Response.
Code for Local JSON file
searchData() {
const ucv_data: any = 'src/assets/json/ucv_json.json';
this.http.get(ucv_data).subscribe((res) => {
this.data = res;
console.log('Response Returned');
},
err => {
console.log('Error Response');
}
);
}
Output: Error Response Expected: Response Returned
Excerpt from Angular.json file
{
// Angular configuration details
}