Recipe 4.1
Sending a Request With XMLHttpRequest
The XMLHttpRequest
API is an event-based API used to send asynchronous HTTP requests. Despite its name, it is most commonly used to request JSON data.
It’s an older API that isn’t as elegant to work with as an alternative like the Fetch API, but still works perfectly well when needed.
Demo
First name | Last name | Department |
---|---|---|
Loading...
Loading Users
|
Code
JavaScript
function loadUsers() {
const request = new XMLHttpRequest();
request.addEventListener('load', event => {
// The event target is the XHR itself; it contains a
// responseText property that we can use to create a JavaScript object from
// the JSON text.
const users = JSON.parse(event.target.responseText);
console.log('Got users:', users);
document.querySelector('#loader').remove();
renderUsers(users);
});
// Handle any potential errors with the request.
// This only handles network errors. If the request
// returns an error status like 404, the `load` event still fires
// where you can inspect the status code.
request.addEventListener('error', err => {
console.log('Error!', err);
});
request.open('GET', `/api/users`);
request.send();
}
loadUsers();
/**
* Renders an array of users in the user table.
* @param userList the array of users
*/
function renderUsers(userList) {
const tableBody = document.querySelector('#users tbody');
userList.forEach(user => {
renderUser(user, tableBody);
});
}
/**
* Renders a user object as a row in the user table.
* @param user the user object to render
* @param tableBody the table body to append the row to
*/
function renderUser(user, tableBody) {
const row = document.createElement('tr');
const firstName = document.createElement('td');
firstName.textContent = user.firstName;
row.appendChild(firstName);
const lastName = document.createElement('td');
lastName.textContent = user.lastName;
row.appendChild(lastName);
const department = document.createElement('td');
department.textContent = user.department;
row.appendChild(department);
tableBody.appendChild(row);
}
HTML
<table id="users" class="table">
<thead>
<tr>
<th class="w-25">First name</th>
<th class="w-25">Last name</th>
<th class="w-25">Department</th>
</tr>
</thead>
<tbody>
<tr id="loader">
<td colspan="3" class="text-center p-4">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<div>Loading Users</div>
</td>
</tr>
</tbody>
</table>