Skip to content

Instantly share code, notes, and snippets.

@JoaquinRuiz
Created February 22, 2019 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoaquinRuiz/89679f9f959c829c2318a2d1d18da845 to your computer and use it in GitHub Desktop.
Save JoaquinRuiz/89679f9f959c829c2318a2d1d18da845 to your computer and use it in GitHub Desktop.
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