0

Here is my code:

<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
    $(function() {
        var thing = [];
        var bar = $.getJSON('C:\Users\cccompro\foo.json', function(obj) {
            for (i = 0; i < obj.length; i++) {
               thing.push(obj[i]);
            }
        });
    });
</script>

I'm not sure why it doesn't work. "foo.json" contains an array of objects.

cccompro
  • 55
  • 6
  • You don't seem to be doing anything with `thing`. How do you know it's not working? – Niet the Dark Absol Jul 18 '17 at 14:39
  • what do you see when you console.log(obj) – brk Jul 18 '17 at 14:39
  • 1
    You can't read/write local files with your browser like that. You need to serve `foo.json` from a webserver. – Gillespie Jul 18 '17 at 14:41
  • Possible duplicate of [How can I get javascript to read from a .json file?](https://stackoverflow.com/questions/6711002/how-can-i-get-javascript-to-read-from-a-json-file) – Heretic Monkey Jul 18 '17 at 14:47
  • At which browser are you trying? Chrome or Chromium? – guest271314 Jul 18 '17 at 14:58
  • @RPGillespie _"You can't read/write local files with your browser like that."_ That is not entirely accurate. Yes it is possible to request files from local filesystem. And technically, it is possible to write to local filesystem using browser technologies. – guest271314 Jul 18 '17 at 14:58
  • As a note: `html` is a markup language, in the html5 standard a js API is defined, but reading is done using js and not html. – t.niese Jul 18 '17 at 15:42
  • @guest271314 Yes, but clearly OP is a beginner and isn't understanding basic web technologies. Instead of pointing him to the technically-correct solution to X problem, I saw this as a potential XY problem and brought his attention to the fact that generally speaking you need a webserver to accomplish the common task of fetching a file. – Gillespie Jul 18 '17 at 18:45
  • @RPGillespie OP is trying to try processes and develop locally. See [--allow-file-access-for-files](http://peter.sh/experiments/chromium-command-line-switches/#allow-file-access-from-files) _"By default, file:// URIs cannot read other file:// URIs. This is an override for developers who need the old behavior for testing."_ A web server is not needed to develop locally; a browser can be used as a server; files can be requested from local filesystem. OP may simply not be aware of the procedures necessary to develop locally at certain browsers. – guest271314 Jul 19 '17 at 00:40

2 Answers2

1

You cannot read files directly from the users hard drive without the browsers permission. This would be a huge security issue if you could even though there are ways to allow this (checkout guests answer).

You could however try to make the user select the file and then read it with Javascript. This is called the HTML 5 file API.

However, this doesn't work for any browser and you probably have to use a server anyway in this case.

For more information on this checkout this or this post.

Spitzbueb
  • 5,233
  • 1
  • 20
  • 38
  • _"You cannot read files directly from the users harddrive"_ It does not appear that OP is trying to read files from another users hard drive, but from their own hard drive, which is possible – guest271314 Jul 18 '17 at 15:01
  • @guest271314 As far as I know there isn't a unique nor clean way that works on all browsers. I think using the HTML 5 file API is the best approach. If you know another way I would like to hear it :) – Spitzbueb Jul 18 '17 at 15:17
  • Agree that using File API is most universal approach for OP's specific requirements. OP appears to be using Chrome or Chromium, or they would not be receiving error (which they did not describe) at Firefox. See post below for several options at chrome, chromium. It is also possible to load a file at a `document` using `` element – guest271314 Jul 18 '17 at 15:22
  • Write/Read/Write using File API [Edit, save, self-modifying HTML document; format generated HTML, JavaScript](https://stackoverflow.com/questions/30563157/edit-save-self-modifying-html-document-format-generated-html-javascript?) – guest271314 Jul 18 '17 at 15:31
1

If you are trying the code at Question at Chrome or Chromium browsers, launch the browser instance with --allow-file-access-from-files flag set. Note that open instances of Chrome or Chromium should be closed when you launch the browser or the instance will be launched with the open browser instances' configuration folder, instead of with the flag set. You can launch Chrome or Chromium with an existing instance open and honoring the flag by using --user-data-dir flag with value set a different directory than open instance of Chrome or Chromium.

Technically, it is also possible to write to user file system without using an extension with window.webkitRequestFileSystem. Though using chrome.fileSystem within an extension provides an API designed to achieve the read/write.

See

Using <input type="file"> element

guest271314
  • 1
  • 15
  • 104
  • 177