+ 2
How to read local Json files with jquery
I searched a lot for a way ro read local files, particularly json files, that are stored locally in the user machine. For particular reasons I want to use java script to generate a user interface based on same data store locally (NOT IN THE SERVER) of the user. I read many answers in stackoverflow. But there is nothing straight forward. Without any trick like converting your machine in a server and make a kind of http request etc etc. So the question is simple. Is it possible? What is the easiest way?
4 ответов
+ 3
If you don't like to use ajax method, there is no other option to auto load external file in browser JavaScript, except you wish to use local Nodejs to read json file and parse and inject it into the public html via an ejs file.
However, you could manually load a local json file, using input tag with type=file manual method. Here the code to illustrates manually load the json, then use javascript to parse it into json object before update to DOM.
https://code.sololearn.com/WKI5j0x65C7t/?ref=app
Note that you require to run the code from pc, and make sure load a proper json object file.
e.g.
json text file:
{
"name": "Calvin",
"job": "Web developer"
}
+ 2
Files in the client machine? It's only possible with file input.
When speaking about local files you are referring to your localhost. Your server.
Where the only solution would be through http requests.
0
1. open the file.. assign the entire JSON to a variable
2.include the JSON in html
3. directly use it the variable in any script that follows
0
A) user machine files i am referring to. Not server side.
B) including the json in the html file it's not a solution since the htln file would dynamically generate a Web site base on the data of every user. This data can not leave a local computer it's sensible data.
There are a lot of solutions for server side. But not for reading the files in the user machin.