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.
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.
InvalidStateErrorresponseType 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 The definition of 'responseXML' in that specification. | Living Standard | WHATWG living standard |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | Yes | 12 | Yes
|
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 | Yes | Yes |
XMLHttpRequestXMLHttpRequest.responseXMLHttpRequest.responseTypeDOMParser
XMLSerializer (specifically, the serializeToString() method)
© 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