Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

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?
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

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
you could also of course just reference the file in plain old script tag..
Avatar of JElster

ASKER

It's a local mobile app
not sure what you mean by 'local'.

Does it run in the browser, or is there some kind of wrapper like phonegap?
Avatar of JElster

ASKER

Just looking for code and suggestions...  uses cordova too..
SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

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
You can't. You have to select it.
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.