W3cubDocs

/DOM

XMLHttpRequest.responseXML

The XMLHttpRequest.responseXML property is a read-only value which returns a Document containing the HTML or XML retrieved by the request, or null if the request was unsuccessful, has not yet been sent, or if the retrieved data can't be correctly parsed as XML or HTML. The response is parsed as if it were a "text/xml" stream. When the responseType is set to "document" and the request has been made asynchronously, the response is parsed as a "text/html" stream. responseXML is null for any other types of data, as well as for data: URLs.

Note: The name responseXML is an artifact of this property's history; it actually works for both HTML and XML.

If the server doesn't specify the Content-Type header as "text/xml" or "application/xml", you can use XMLHttpRequest.overrideMimeType() to force XMLHttpRequest to parse it as XML anyway.

Syntax

var data = XMLHttpRequest.responseXML;

Value

A Document containing the nodes resulting from parsing XML or HTML received using XMLHttpRequest, or null if no data has been received or the data is not of the correct type.

Exceptions

InvalidStateError
The responseType isn't either "document" or an empty string (either of which indicates that the received data is XML or HTML).

Example

var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);

// If specified, responseType must be empty string or "document"
xhr.responseType = 'document';

// overrideMimeType() can be used to force the response to be parsed as XML
xhr.overrideMimeType('text/xml');

xhr.onload = function () {
  if (xhr.readyState === xhr.DONE) {
    if (xhr.status === 200) {
      console.log(xhr.response);
      console.log(xhr.responseXML);
    }
  }
};

xhr.send(null);

Specifications

Specification Status Comment
XMLHttpRequest
The definition of 'responseXML' in that specification.
Living Standard WHATWG living standard

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes 12 Yes
Yes
Prior to Firefox 51, an error parsing the received data added a <parsererror> node to the top of the Document and then returned the Document in whatever state it happens to be in. This was inconsistent with the specification. Starting with Firefox 51, this scenario now correctly returns null as per the spec.
Yes Yes Yes
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes Yes Yes
Yes
Prior to Firefox 51, an error parsing the received data added a <parsererror> node to the top of the Document and then returned the Document in whatever state it happens to be in. This was inconsistent with the specification. Starting with Firefox 51, this scenario now correctly returns null as per the spec.
Yes Yes Yes

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML