Skip to content

Instantly share code, notes, and snippets.

@chuoique
Forked from pwittchen/loadJson.js
Created February 16, 2019 04:23
Show Gist options
  • Save chuoique/03d787724da162fd32b0196dbeafdd2a to your computer and use it in GitHub Desktop.
Save chuoique/03d787724da162fd32b0196dbeafdd2a to your computer and use it in GitHub Desktop.

Revisions

  1. @pwittchen pwittchen created this gist Apr 19, 2014.
    18 changes: 18 additions & 0 deletions loadJson.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    function loadJson(callback) {
    var XmlHttpRequest = new XMLHttpRequest();
    XmlHttpRequest.overrideMimeType("application/json");
    XmlHttpRequest.open('GET', 'file.json', true);
    XmlHttpRequest.onreadystatechange = function () {
    if (XmlHttpRequest.readyState == 4 && XmlHttpRequest.status == "200") {
    // .open will NOT return a value
    // but simply returns undefined in async mode so use a callback
    callback(XmlHttpRequest.responseText);
    }
    }
    XmlHttpRequest.send(null);
    }

    loadJson(function(response) {
    jsonResponse = JSON.parse(response);
    console.log(jsonResponse);
    });