Is it possible to handle data from a JSON file without having it served via http:// or https:// ? Basically like you reference a JS file or CSS file in the HTML page?
-
1JS and CSS files are also handled via http. What are you trying to achieve. Be more precise please. – Markus Kottländer Dec 25 '13 at 14:21
-
My ultimate aim is to run this from a folder, allowing me to access it via file:// instead of having it served via http protocol. @MarkusKottländer – Dragan Marjanovic Dec 25 '13 at 14:24
-
To develop standalone web-apps, have a look at [jetty](http://www.eclipse.org/jetty/) - a standalone webserver which can be included in one executable jar.. double-click it on every java-enabled machine und start surfing at http://localhost:8080 for example – jebbie Dec 25 '13 at 14:27
2 Answers
As jebbie's answer noted, if you wish to run your app as a webapp in a browser, the only way to access local file (JSON does not matter, any data file has same limitations) is via file://
protocol OR with HTML5's Local Storage. That imposes its own constraints.
However, a LOT simpler and better solution all-around[1] is to install a simple local webserver (heck, installing Apache which is far from simplest takes less than 30 minutes). There are webservers where entire install means download, edit 1 config file and run the executable. One typical recommendation is Mongoose, more are here: Best lightweight web server (only static content) for windows
Once you do that, you can place your data file (including JSON) into a web docs directory and access as you would any other web resource (ultimately, coming from http://localhost:80/
or whatever your local http port you set up).
[1] Web server is better because then (1) you learn web development skills that are transferable to real web apps running on web server and (2) because if your mini app is actually useful, you can reuse some/all of it in later projects
-
Yeah no worries, I was just hoping it'd be possible, oh well, one day maybe. – Dragan Marjanovic Dec 25 '13 at 14:35
-
what comes in my mind when i read mongoose - [mongodb](http://www.mongodb.org/) a really nice no-sql database which stores directly json documents, extremely fast, scalable and also a very fast setup.. you don't need to struggle with ERM and table definition, you just start coding and you create your db and tables (collections) on-the-fly :) – jebbie Dec 25 '13 at 14:35
-
-
@jebbie I was looking at Couch DB, which also seems quite promising. I'll definitely look into mongo aswell, thanks =) – Dragan Marjanovic Dec 25 '13 at 14:38
-
Just for reference, FireFox's internal data is stored as simple SQLite DB :) – DVK Dec 25 '13 at 14:40
-
yeah i tend to over-engineer, even in small projects xD i had never a look in CouchDB because my working-colleagues had problems to solve some special problems with it, now we're running on mongodb.. but i believe both projects are about the same quality :) – jebbie Dec 25 '13 at 14:40
-
Mongo seems to have a nicer overview / explanation of the documentation, I'll go with it aha, Thanks – Dragan Marjanovic Dec 25 '13 at 14:46
Basically - nope. First of all, also when you reference a JS or CSS file, it is always served over http:// or https://, the browser will load everything you give him with the http-protocol, the only one he knows.. if you don't believe me, have a look in your network tab of the developer tools and you will see there every single file loaded over the network..
the only other way to load local files is the file:// protocol, which is not recommended because your browser falls in a sandbox-mode where not everything is possible to do.
But storing JSON data locally.. that's kind a new stuff which is possible to achieve with localStorage of HTML5 ;-)

- 1,418
- 3
- 17
- 27
-
Yeah, this was meant to be a simple little project via file:// protocol. Even if I did store my JSON locally, how would I then go about accessing it? – Dragan Marjanovic Dec 25 '13 at 14:26
-
1localStorage['myData'] = JSON.stringify(myHugeJsonObject); var myHugeJson = JSON.parse(localStorage['myData']) – jebbie Dec 25 '13 at 14:29
-
the problem is, you can't place a file in your local storage prior you access a domain.. it's like a cookie.. you still have to start at a page/domain that is served from a webserver.. then you can start inside thats site js-code to save stuff into localStorage and access it later again, the data is stored on your hard-drive. – jebbie Dec 25 '13 at 14:38