responseXML always null

Viewed 54939

Im using firefox 3.6.10, and firebug to debug

So, here is my code:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url,false);
xmlhttp.setRequestHeader('Content-Type',  'text/xml');
xmlhttp.send(null);
alert(xmlhttp.responseXML);

responseXML is always null, and i've tried it on several URLs from different domains. I have also tried it asynchronously, it's the same result. The responseText is always properly returned, no problems with it.

My goal is to get the responseXML.documentElement.

Thanks for your help.

EDIT-----------
This javascript code was executed from a Greasemonkey userscript, i made surte its the same origin as the requested url. Also i tried executing from firebug console, again ensuring the origin policy. Same error on both.
Gotta hate javascript.

9 Answers

I just had the same problem.

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url,false);
xmlhttp.setRequestHeader('Content-Type',  'text/xml');

xmlhttp.responseType = "document";

xmlhttp.send(null);
alert(xmlhttp.responseXML);

Add responseType "document" line to your code to fix this. Goes between open and send methods.

Reference: (See USAGE) https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/HTML_in_XMLHttpRequest

I had a hard time to find a correct xml example. If you get null try an XML Validator. For me the <root> element was missing.

<root>
<info><p>Array
(
)
</p>
</info>
<itemData>{"id":"40","client_id":"1","nameUnique":"Lore ipsum","description":null,"userComment":null,"last_modified":"2018-12-15 02:48:57"}    </itemData>
</root>
Related