Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created November 30, 2020 17:58
Show Gist options
  • Select an option

  • Save wpflames/3335ebcc56728dba0139688e3d33b8e4 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/3335ebcc56728dba0139688e3d33b8e4 to your computer and use it in GitHub Desktop.
AJAX request for JSON file (local) - Array
var btn2 = document.getElementById('button2');
btn2.addEventListener('click', loadUsers);
//REQUEST ALL USERS DATA
function loadUsers(){
var xhr = new XMLHttpRequest();
xhr.open('GET', 'assets/json/users.json', true);
xhr.onload = function(){
if(this.status = 200){
var users = JSON.parse(this.responseText);
var output = '';
for(var i in users){
output += '<ul>' +
'<li>ID: '+ users[i].id +'</li>' +
'<li>Name: '+ users[i].name +'</li>' +
'<li>Email: '+ users[i].email +'</li>' +
'</ul>';
}
document.getElementById('users').innerHTML = output;
}
}
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment