Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AJAX and JSON guide for beginners
<button type="button" onclick="getJSON()">Get the users</button>
<table id="users"></div>
<script>
function getJSON() {
var ajax_req = new XMLHttpRequest();
ajax_req.open('GET', 'https://api.github.com/users', true);
ajax_req.onload = function() {
//status 200 means everything is ok
if (this.status === 200) {
var json = JSON.parse(this.responseText);
var row = '';
for (i in json) {
row += '<tr><td>' + json[i].login +
'</td><td><img src="' +
json[i].avatar_url + '</td></tr>';
}
document.getElementById('users').innerHTML = row;
}
}
ajax_req.send();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.