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.
The name responseXML
is an artifact of this property's history; it really does work 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.
var data = XMLHttpRequest.responseXML;
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.
InvalidStateError
responseType
isn't either "document"
or an empty string (either of which indicates that the received data is XML or HTML).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);
Specification | Status | Comment |
---|---|---|
XMLHttpRequest | Living Standard | WHATWG living standard |
Feature | Chrome | Edge | Firefox (Gecko)[1] | Microsoft Edge | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | (Yes) | (Yes) | ? | (Yes) | (Yes) |
[1] 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.
© 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