Created
November 30, 2020 17:58
-
-
Save wpflames/3335ebcc56728dba0139688e3d33b8e4 to your computer and use it in GitHub Desktop.
AJAX request for JSON file (local) - Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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