AngularJS - how to read a local (resource?) JSON file

Hi..
I need to read a local JSON file that contains mapping info.
What the correct/best way to read it?
Do I use HTTP GET?  or Resource?
LVL 1
JElsterAsked:
Who is Participating?
 
ambienceConnect With a Mentor Commented:
HTML5 has new API that lets you interact with local files. You can find dozens of examples, here are a few:

http://blog.teamtreehouse.com/reading-files-using-the-html5-filereader-api
http://www.html5rocks.com/en/tutorials/file/dndfiles/
0
 
Kyle HamiltonData ScientistCommented:
local on someone's computer through a web app? you cant with a web browser.

imagine the security nightmare if websites could have access to your computer's files!

you could have a button that loads the file, but the user would have to select the file.

if the question is about developing your app on your local computer, then in order to make ajax requests you would need to 'host' your app. in other words, you need a localhost, or virtual host on your computer to serve your app
0
 
Kyle HamiltonData ScientistCommented:
you could also of course just reference the file in plain old script tag..
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
JElsterAuthor Commented:
It's a local mobile app
0
 
Kyle HamiltonData ScientistCommented:
not sure what you mean by 'local'.

Does it run in the browser, or is there some kind of wrapper like phonegap?
0
 
JElsterAuthor Commented:
Just looking for code and suggestions...  uses cordova too..
0
 
Kyle HamiltonConnect With a Mentor Data ScientistCommented:
0
 
JElsterAuthor Commented:
So how do I read a local JSON file without having to select it?
Something like this>

      var reader = new FileReader();

                  var file = 'data/myJSON.json'

                        reader.readAsDataURL(file);      


thx
0
 
Kyle HamiltonData ScientistCommented:
You can't. You have to select it.
0
 
ambienceCommented:
There is no standard way of reading a file in such a covert manner. The only way I can think of is to write an ActiveX control, which would also considerably limit the target OS and browsers.

You can however use webstorage and WebSQL (all new goodies that come with HTML5) and save content locally. This should however should only be treated as a caching mechanism and you cannot rely on the content to be always there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.