I'm creating a simple webapp. I have a json file, with some datas I need to be processed using javascript ( using javascript and not python to avoid the post request and the page reloading). I have both the js file and the json file in the "/static" directory of the Flask enviroment. Now, I would like to know how to import the json file. I tried using fetch this way:
fetch("test.json")
.then(response => response.json())
.then(json => console.log(json));
But it failed. I tried to read the file via python and the pass it in javascript with the following code:
with open("file.json", "r") as data:
print(data)
But it said me that that file wasn't found. I also read some other sackoverflow answer, and read Json documentation, without finding anything. Some ideas? Thanks
It’s easy if you know what separation of front-end and backend and http protocol.
Your JavaScript code maybe need some change ... ,like this fetch("/static/test.json") .then(response => response.json()) .then(json => console.log(json));
Import json on the server is not appropriate