XMLHttpRequest: readystatechange event

The readystatechange event is fired whenever the readyState property of the XMLHttpRequest changes.
Warning: This should not be used with synchronous requests and must not be used from native code.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener('readystatechange', (event) => { })

onreadystatechange = (event) => { }

Event type

A generic Event with no added properties.

Examples

const xhr = new XMLHttpRequest();
const method = "GET";
const url = "https://developer.mozilla.org/";

xhr.open(method, url, true);
xhr.onreadystatechange = () => {
  // In local files, status is 0 upon success in Mozilla Firefox
  if (xhr.readyState === XMLHttpRequest.DONE) {
    const status = xhr.status;
    if (status === 0 || (status >= 200 && status < 400)) {
      // The request has been completed successfully
      console.log(xhr.responseText);
    } else {
      // Oh no! There has been an error with the request!
    }
  }
};
xhr.send();

Specifications

Specification
XMLHttpRequest Standard
# event-xhr-readystatechange
XMLHttpRequest Standard
# handler-xhr-onreadystatechange

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
readystatechange event

Legend

Tip: you can click/tap on a cell for more information.
Full supportFull support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.