Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-09
10
Medium Priority
?
1,472 Views
Last Modified: 2015-01-12
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?
0
Comment
Question by:JElster
  • 5
  • 3
  • 2
10 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40541955
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
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40541959
you could also of course just reference the file in plain old script tag..
0
 
LVL 1

Author Comment

by:JElster
ID: 40541987
It's a local mobile app
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40541998
not sure what you mean by 'local'.

Does it run in the browser, or is there some kind of wrapper like phonegap?
0
 
LVL 1

Author Comment

by:JElster
ID: 40542001
Just looking for code and suggestions...  uses cordova too..
0
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 1000 total points
ID: 40542018
0
 
LVL 22

Accepted Solution

by:
ambience earned 1000 total points
ID: 40544023
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
 
LVL 1

Author Comment

by:JElster
ID: 40544351
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
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40544366
You can't. You have to select it.
0
 
LVL 22

Expert Comment

by:ambience
ID: 40544442
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question