1

I am having a hard time finding an example how to read a simple .json file and I can read and create a JSON object. Here is my file named AllWeek.json

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [101.46260261535645, 3.0185554329110373]
      },
      "properties": {
        "_id": "AVSsYt-dJhbPevXZmtga",
        "area": "candar kuteri plang",
        "square_feet": 326
      }
   }


  ]
 }

I have tried this code but it gives me CORS error.

readTextFile("AllWeek.json", function(text) {
  var data = JSON.parse(text);
  console.log(data);
});

function readTextFile(file, callback) {
  var rawFile = new XMLHttpRequest();
  rawFile.overrideMimeType("application/json");
  rawFile.open("GET", file, true);
  rawFile.onreadystatechange = function() {
    if (rawFile.readyState === 4 && rawFile.status == "200") {
      callback(rawFile.responseText);
    }
  };
  rawFile.send(null);
}
Sarah
  • 1,199
  • 2
  • 21
  • 42
  • A CORS error would be related to trying to make an ajax request cross domain. Your ajax request method does not appear to be going cross domain. Is this the actual url that you are retrieving? – Taplar Jul 17 '20 at 22:32
  • Its just a plain html file that I am rendering and the AllWeek.json is in the same directory – Sarah Jul 17 '20 at 22:33
  • Double check your developer tools network console. If you are going to the same domain, you should not be getting a CORS error – Taplar Jul 17 '20 at 22:35
  • Also a code smell; I'm not entirely sure why you are using `overrideMimeType("application/json")` when your callback is expecting text that it will parse itself. – Taplar Jul 17 '20 at 22:36
  • 2
    You would get CORS error however if you are trying to do this from `file://` protocol without using a localhost server. Lots of ways to quickly and easily set up a localhost server if this is the case – charlietfl Jul 17 '20 at 22:36
  • Updating the code with my entire html/javascript file. And i am only opening the html file in chrome and pasting the error too. – Sarah Jul 17 '20 at 22:38
  • 1
    The JSON is malformed. invalid JSON format content. – toto Jul 17 '20 at 22:40
  • @charlie, yes I am accessing the html just from windows explorer and opening it in chrome.. No localhost. – Sarah Jul 17 '20 at 22:41
  • 1
    So that's (one of) your problem. ^^^ – Taplar Jul 17 '20 at 22:42
  • So you guys are saying that I cant read a file without spinning a server? Humm.. – Sarah Jul 17 '20 at 22:44
  • Or by bypassing browser security restrictions which means they are bypassed for all sites you visit. Spinning up a server is best approach – charlietfl Jul 17 '20 at 22:45
  • 1
    If you are using nodejs it is crazy easy to spin up a server. The duplicate talks about one of the (many) available packages to start up some simple servers. – Taplar Jul 17 '20 at 22:46
  • Really? I am just doing quick and easy javascript html pages and learning. Do you mind explaining why the browser would give this error for a local file on the machine? The html file and JSON files are on the same machine and same folder. CORS error seems very illogical here. – Sarah Jul 17 '20 at 22:47
  • 1
    You should enable allow-file-access-from-files to read local files in Chrome at least. Moreover the JSON is not valid. https://stackoverflow.com/questions/18586921/how-to-launch-html-using-chrome-at-allow-file-access-from-files-mode – Nandu Kalidindi Jul 17 '20 at 22:47
  • @NanduKalidindi just fixed JSON. How can I enable allow-file-access-from-files in Chrome? – Sarah Jul 17 '20 at 22:50
  • @nandu, windows – Sarah Jul 17 '20 at 22:54
  • 2
    Check this link, there is an answer for Windows, https://stackoverflow.com/questions/18586921/how-to-launch-html-using-chrome-at-allow-file-access-from-files-mode However, only use that Chrome instance for your project, better to not open any other websites in that Chrome instance. – Nandu Kalidindi Jul 17 '20 at 22:54
  • @NanduKalidindi, thanks much.. following that. – Sarah Jul 17 '20 at 22:57

0 Answers0