Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Open, send, and read a classic XHR request.
XMLHttpRequest Basics is easiest to learn by reading the example, changing it, and observing the result.
const xhr = new XMLHttpRequest();
xhr.open('GET', '/api/lessons');
xhr.responseType = 'json';
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
console.log(xhr.response);
}
};
xhr.onerror = () => console.error('Network error');
xhr.send();Practice the XMLHttpRequest Basics example in a small scratch file, then explain what changed and why.