0

good afternoon. I'm developing an app that can get a JSON from local (manifest.json). I want to get this file from JavaScript and then read it. But I have a problem, I cant call this file. How can I?

var urlJSON = new XMLHttpRequest("manifes.json").toString;
var dataJSON = JSON.parse(urlJSON);

alert(dataJSON.name);
3
  • First do console.log(urlJSON) to see what actually contains this variable. Commented Sep 24, 2016 at 17:56
  • i think your spelling for filename is wrong var urlJSON = new XMLHttpRequest("manifest.json").toString; Commented Sep 24, 2016 at 17:59
  • That is not how you use XMLHttpRequest, Using XMLHttpRequest. Also what do you mean by "local"? The user's hard drive? That cannot be done, without the user selecting it through some process like a file input Commented Sep 24, 2016 at 17:59

1 Answer 1

2
var xmlhttp = new XMLHttpRequest();
var url = 'manifest.json';
xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    console.log(JSON.parse(xmlhttp.responseText));
  }
  if (xmlhttp.status == 404) {}
};
xmlhttp.open('GET', url, true);
xmlhttp.send();

Or run chrome with arguments --allow-file-access-from-files
Or download and create server for your app

Sign up to request clarification or add additional context in comments.

1 Comment

It says "XMLHttpRequest cannot load file:///C:/Users/ferna/Desktop/The%20pong/manifest.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.