Skip to content

Instantly share code, notes, and snippets.

@auchomage
Created April 20, 2016 10:39
Show Gist options
  • Save auchomage/1f008976d231240640c88a4332d78231 to your computer and use it in GitHub Desktop.
Save auchomage/1f008976d231240640c88a4332d78231 to your computer and use it in GitHub Desktop.
/* This file simulates how to access a native json file*/
var output = document.getElementById('output');
var ajaxhttp = new XMLHttpRequest(); // creates a js object with required functiionality
var url = "json.json"; // the exteranal json file
ajaxhttp.open("GET", url, true); // access the json file
ajaxhttp.setRequestHeader("content-type", "application/json"); // value sent over to the server, indicating
// what type of content the browser expects to receive
/*
readyState - represents communication with an external file
status - a flag used to indicate the success or failure of communication
*/
ajaxhttp.onreadystatechange = function(){
if(ajaxhttp.readyState == 4 && ajaxhttp.status == 200){
var jcontent = ajaxhttp.responseText;
console.log( jcontent); // displays the response text
console.log("ajaxhttp = ", ajaxhttp); // displays the object
}
};
ajaxhttp.send(null); // currently sending nothing back
output.innerHTML ="nothing";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment