XMLHttpRequest, usually shortened to XHR, is a browser API that lets JavaScript running in a page send an HTTP request to a server and process the response without navigating away or reloading. The XMLHttpRequest API helped popularise applications that refresh part of a page while leaving the rest in place. The name is historical: the API can transfer JSON, plain text, HTML, binary blobs or anything else a server returns, despite XML appearing in its name.
XHR is not a network protocol. It is an object inside the browser that constructs an ordinary HTTP request, hands it to the browser's networking stack, and reports progress back to script through events. Browser networking and security rules apply, including TLS certificate validation, cookie policies and restrictions on reading cross-origin responses. An image can sometimes be displayed cross-origin even when XHR cannot expose its contents to script.
Requests, Responses and Events
A typical exchange has four steps. Script creates an XMLHttpRequest object, calls open with a method and a URL, optionally sets request headers, and calls send with an optional body. From that point the object moves through a sequence of ready states: opened, headers received, loading while the body arrives, and done. Each transition fires a readystatechange event, and the object also fires more specific events such as load when a response finishes arriving, error on a network failure, timeout when a configured deadline passes, abort when script cancels the request, and progress as bytes arrive. Upload progress is reported through a nested upload object. A load event can accompany an HTTP error such as 404 or 500, so the handler must examine the status rather than equating completion with success.
The XHR response is exposed through several properties once the request completes. The numeric status gives the HTTP result code; the accompanying status text may be empty. Response headers can be read individually or as a single block, although the browser filters out some of them, such as Set-Cookie, for security reasons. The body is available through a response property whose type depends on what the script asked for before sending: a string by default, or a parsed JSON object, an ArrayBuffer, a Blob or a parsed Document if the responseType property was set accordingly. Asking for the right type up front lets the browser decode the body once instead of forcing script to parse text afterwards.
Timeouts and cancellation are built in. Setting a timeout in milliseconds causes the browser to terminate a slow request and fire the timeout event; calling abort ends a request early. The interface should distinguish a timeout from a rejected request and offer an appropriate recovery action. Aborting locally does not undo work already performed by the server.
Synchronous vs Asynchronous Use
An async HTTP request is the normal mode. The call to send returns immediately, the page keeps rendering and responding to input, and the result arrives later through events. XHR also supports a synchronous mode, selected by a flag on open, in which send blocks until the full response has arrived. On the main thread this blocks JavaScript execution and can make the page unresponsive. It is deprecated there: the specification instructs developers not to use it in a Window context, browsers log warnings, and features such as timeout and non-text response types are refused in synchronous mode.
Synchronous XHR can still be used in supported worker contexts, where it blocks that worker rather than the main thread. Other work assigned to the same worker still has to wait. For sending analytics during page unload, where synchronous requests were once common, browsers now offer the sendBeacon method and the keepalive option on fetch, both of which let a request outlive the page without blocking it.
Origins, Credentials and Browser Policies
XHR follows the same-origin policy. Script on one origin can read the response of a request to another origin only if the response carries Cross-Origin Resource Sharing headers that permit it. The browser still sends the request in many cases, so the server may receive and process it, but the response is withheld from script and an error is reported with no detail about the cause. For requests that use methods or headers outside a short safelist, the browser first sends a preflight OPTIONS request and proceeds only if the server approves.
Credentials are handled conservatively. A cross-origin XHR does not include cookies or HTTP authentication by default. Setting withCredentials to true on the request object asks the browser to include them, which in turn obliges the server to respond with Access-Control-Allow-Credentials set to true and to name a specific origin rather than using a wildcard. Without appropriate permission, script cannot read the response. Credential inclusion and cookie acceptance remain subject to cookie and browser privacy policies; withCredentials cannot override blocked third-party cookies. Same-origin requests are unaffected by the flag; cookies that match the request URL and satisfy their own SameSite and Secure attributes are attached as usual.
Request headers are also under browser control. Script may set custom headers through setRequestHeader, but a defined list of forbidden headers is silently ignored: Host, Origin, Referer, Cookie, Content-Length, anything beginning with Sec- or Proxy-, and others the browser must own to keep its security guarantees intact. For example, adding a custom header is not a way to impersonate another Origin or directly supply an arbitrary Cookie header. Referrer disclosure is also governed by browser referrer policy.
XMLHttpRequest vs Fetch
The fetch API was designed as the successor, and in modern browsers XHR is itself specified in terms of the same underlying fetch algorithm. The differences that matter to developers are mostly about ergonomics and streaming.
| Aspect | XMLHttpRequest | fetch |
|---|---|---|
| Result handling | Events and callbacks | Promises, works with async/await |
| Progress events | Download and upload progress built in | Download via readable streams; no native upload progress |
| Streaming | Body available only when complete, except text in loading state | Response body is a stream readable incrementally |
| Cancellation | abort method | AbortController and signal |
| Timeout | timeout property | AbortController or an AbortSignal timeout |
| Synchronous mode | Deprecated but present | Not available |
| Credentials default | Excluded cross-origin | same-origin mode by default |
| Availability | Window and supported workers; not service workers | Window, workers and service workers |
In the XHR vs fetch choice, promise-based code and streaming often favour fetch. The exception is upload progress reporting, which fetch still cannot provide natively, so file-upload widgets that show a progress bar often remain on XHR. Both APIs face identical CORS and cookie rules, so switching between them changes how code is written, not what the browser permits.
Where XHR Still Appears and Its Limits
XHR remains everywhere in deployed code: older single-page applications, embedded analytics tags, advertising scripts, and polyfills. Developer tools commonly provide an XHR/fetch filter for inspecting background HTTP requests. Automation and testing frameworks intercept XHR alongside fetch when mocking server responses, and network-level monitoring can observe both without replacing either API.
Its limits are those of any browser request. XHR cannot bypass cross-origin response restrictions, set forbidden headers or expose HttpOnly cookie values. Applications can implement polling or long polling with it; EventSource and WebSocket provide interfaces designed for ongoing server messages. Service workers use fetch rather than XHR. And because XHR is an API rather than a transport, it does not create a special anonymous connection. Requests use HTTP semantics, normally protected by TLS for HTTPS URLs. The destination sees the submitted request, while an ordinary network observer cannot read encrypted headers and bodies without access to the TLS session.