AJAX and JSON guide for beginners
This file contains 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
<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