This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

Fetch a JSON file with XMLHttpRequest

Content written as an JSON array will be converted into a JavaScript array.

// create XMLHttpRequest function
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {

// create myArr and display myArr[0] index value
  const myArr = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myArr[0];
};

// define XMLHttpRequest object
xmlhttp.open("GET", "json_demo_array.txt", true);
xmlhttp.send();