Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Read and maintain older AJAX code that still uses XMLHttpRequest.
XMLHttpRequest for Legacy Code 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 for Legacy Code example in a small scratch file, then explain what changed and why.