XMLHttpRequest
XMLHttpRequest is the older browser API for HTTP. It is event-based (onload, onerror, onprogress) rather than promise-based. fetch replaced it for most code.
Detailed Explanation
Before fetch, AJAX meant XHR. You construct new XMLHttpRequest(), open(method, url), set headers, send(body), and listen for load / error / abort. responseText and response hold the body. status is the HTTP code. withCredentials is the cookie flag (like credentials: "include").
XHR still wins at upload progress: xhr.upload.onprogress reports bytes sent. fetch upload progress is incomplete in browsers (you can stream a body but progress events are not the same). Download progress is xhr.onprogress when the server sends Content-Length.
The API is easy to get wrong: send before open throws, headers after send throw, and a JSON parse of responseText is your job. Wrapping XHR in a promise is a few lines and is how many libraries still expose it.
fetch cannot be fully polyfilled by XHR (streams, keepalive, abort semantics differ). In modern apps, reach for XHR only when you need upload progress or must support an engine without fetch (rare).
What it costs you: callback soup and a second HTTP stack in the codebase. Prefer fetch plus a small progress design (chunked uploads with your own counters) if you can.
Reach for XHR for upload bars on older stacks. Reach for fetch for everything else. Reach for a library (axios) only if the team already standardised on it — it is XHR under the hood in browsers.
timeout on XHR is a property in milliseconds; fetch has no equivalent except AbortSignal.timeout(ms) in modern browsers. If you still wrap XHR, set timeout and handle ontimeout as its own error, not as onerror.
Synchronous XHR (open(..., false)) is deprecated and blocks the thread. It will not work in some contexts (main page in modern Chrome). Never add it back for a "simpler" call.
withCredentials plus a wildcard CORS origin fails the same way fetch credentials do. The rules are the browser’s, not the API’s.
Code Examples
function getText(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) resolve(xhr.responseText);
else reject(new Error(`http ${xhr.status}`));
};
xhr.onerror = () => reject(new Error("network"));
xhr.send();
});
}function upload(url, file, onProgress) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.upload.onprogress = (event) => {
if (event.lengthComputable) onProgress(event.loaded / event.total);
};
xhr.onload = () => resolve(xhr.status);
xhr.onerror = () => reject(new Error("network"));
const body = new FormData();
body.append("file", file);
xhr.send(body);
});
}Common Gotchas
`onerror` is network-level
HTTP 500 still fires `onload` with a bad `status`. Check `status` in `onload`.
`open` is synchronous setup; `send` starts the request
listeners should be attached before `send`.
Prefer `fetch` unless you need `upload.onprogress`
Prefer `fetch` unless you need `upload.onprogress`