Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot fetch local json file, file path error (using a live server already)

Suppose my current file structure with 3 following files is:

main/index.html  
main/res/json/myjson.json  
main/src/js/script.js

this is in my index header:

<script type="text/javascript" src="src/js/script.js"></script>

In my JavaScript file, I try to fetch the json file with these lines:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 fetch('../../res/json/mjson.json')
        .then((response)
        .then(json => {
       
       ...
       ...

I run the index.html with a live server plugin for VSCode. However, no matter what I do, it returns me a 404 Not Found error that the json resource is not found. Absolute path gives an error aswell, cannot test my script with than even. Any solutions to this besides hosting an API?

>Solution :

Why are these here?: ../../

Consider how you reference the JavaScript file from your page:

src/js/script.js

Reference the JSON file the same way:

res/json/myjson.json

Both are relative to the URL of the page shown in the browser.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading