-2

Is it possible to set a variable in JavaScript to a local JSON file stored on computer?

var data = c/path/path/data.json
Biffen
  • 6,249
  • 6
  • 28
  • 36
Anders Pedersen
  • 2,255
  • 4
  • 25
  • 49

2 Answers2

1

If you are asking if you can access the file system with JavaScript the answer is yes and no. If you are using a tool like node.js then yes you can access the file system using JavaScript. If you are trying to access the file system from the browser then no JavaScript does not natively have that capability.

It really does not matter what you are trying to access on the file system. It could be JSON, jpg, or gif... If you are using a browser it is not possible..

However you can make ajax call to a server and get files that way. i.e. JSON files... You can also store information using JavaScript using the 'localstorage' method built into JavaScript.

Biffen
  • 6,249
  • 6
  • 28
  • 36
Tim
  • 1,606
  • 2
  • 21
  • 42
0

You can make an Ajax request to get the external json.

$.ajax({
  url: "c/path/path/data.json",
}).done(function(JsonToGet) {
  var data = JsonToGet;
  //Management of the JSON with 'data' variable. 
});
  • This will not work on any main stream browsers. Some browsers let you disable security and allow it but in most circumstances it will not work. – Tim Apr 13 '16 at 13:00
  • [Try to follow this link! It may explain better... **using-ajax-to-read-local-files**](http://stackoverflow.com/questions/6923707/using-ajax-to-read-local-files) – Tim Apr 13 '16 at 13:07
  • You're right. The way I write is valid for a Rest web service, for example. Not recommended for local files. – SirJavascript Apr 13 '16 at 13:12