function search() {
// Send an XhR2 query. See HTML5 games and Apps MOOC at W3Cx
// for detailed explanation about XhR2.
var queryURL = "https://jsonplaceholder.typicode.com/users";
var xhr = new XMLHttpRequest();
xhr.open('GET', queryURL, true);
xhr.onload = function(e) {
var users = JSON.parse(xhr.response);
displayUsersAsATable(users);
}
xhr.send();
}
function displayUsersAsATable(users) {
// users is a JavaScript object
// empty the div that contains the results
var usersDiv = document.querySelector("#users");
usersDiv.innerHTML = "";
// creates and populate the table with users
var table = document.createElement("table");
users.forEach(function(currentUser) {
var row = table.insertRow();
row.innerHTML = "<td>"+ currentUser.name+ "</td><td>"
+ currentUser.email + "</td>";
});
usersDiv.append(table);
}
JS.JSON.AJAX.Request.Live coding video, JSON with the XhR2 API
--------------------------------------------------------------
A [Pen](https://codepen.io/Onlyforbopi/pen/OBLpLM) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).
[License](https://codepen.io/Onlyforbopi/pen/OBLpLM/license).
<!DOCTYPE html>
<html lang="en">
<head>
<title>Working with remote data using XhR2</title>
<meta charset="utf-8"/>
</head>
<body>
<p>Working with remote data suing XhR2</p>
<button onclick="search();">Get a remote list of users' names and emails</button>
<div id="users"></div>
</body>
</html>