23

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 9

26

If i recall correctly , this is a known problem with firefox ( i have had the same problem before ).

The fix is to parse the responseText back to an XML document , and then use this.

Something like this :

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
5
  • From MDC: DOMParser can be used to parse strings and streams of XML text. It can't be used to parse HTML "tag soup". However a link on that same page should point you in the right direction.
    – MooGoo
    Sep 23, 2010 at 18:41
  • Obviously : what you are trying to read should be correct XML . That's the case i used it with. Not sure what happens in the case of XHTML ( though it's valid XML , so it should work ).
    – Kenny
    Sep 23, 2010 at 18:52
  • This has just cropped up in Chrome (32.0.etc.etc) which was working fine previously. Cheers for the fix
    – graham
    Jan 17, 2014 at 17:05
  • And this problem is still happening in 2016, but thank you, this method fixed my page!
    – Phantom
    Oct 3, 2016 at 1:20
  • Useful for nowadays. The samples that I saw using reponseXML straitffarward seems to belong in ancient days of web. May 2, 2020 at 13:20
26

Besides the cross-domain issues already mentioned, responseXML requires completely valid XML and probably the correct Content-Type in the response headers sent from the server. It is very unlikely that either of these requirements would be met by the average website.

For the latter issue, you can use

xmlhttp.overrideMimeType('application/xml');

before you send the request to force the response to be interperted as XML. Still if the response is not valid XML, you will only get null.

6
8

I bet you are violating the same origin policy.

For XHRs, you must have the same protocol, domain, port, etc. So if you are running an app on localhost:8080/app, you CANNOT ajax to www.cnn.com.

Different browsers handle this differently; I have seen FF do what you describe, which is the request appears to return normally but there is no data...

2
  • 1
    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.
    – WoF_Angel
    Sep 23, 2010 at 18:33
  • You're right. There are some workarounds for this, i may check them later.
    – WoF_Angel
    Aug 4, 2011 at 11:28
6

Try to open the value of url directly in the browser. You should get some error information.
If you see a parsing error, chances are your encoding is wrong and you have a special character in your XML that makes it invalid.

To avoid that, you need to be sure that all the chain is properly encoded.

If it is a static XML file, you need to set correctly your editor encoding when saving it. The encoding that does it all(almost) is UTF-8, it is usually a property you can choose in your editor settings or in the save dialog.

If it is dynamically generated. Your data, the page and the server response must be properly encoded too. And your XML starting with <?xml version="1.0" encoding="UTF-8"?>

You can try first with a very basic and static XML:

<?xml version="1.0" encoding="UTF-8"?><root>hi</root>

And then add the steps, one by one to make it like yours, without breaking it.

2
  • I have no access at all to the server, this is supposed to be a greasemonkey script.
    – WoF_Angel
    Sep 23, 2010 at 23:54
  • Try with the basic XML above on your localhost to see if your script works. And then type the other server url directly in the address bar of your browser to see what happen. And then try to get the encoding of the XML your receive from the server... I remember loosing hours making SAP generating me an XML doc and not a string until I figured out it was about the encoding. Good luck!
    – Mic
    Sep 23, 2010 at 23:59
1

Was fooling around with this for hours and finally figured out the stupid little error that was messing me up...

If you are like me, you like to keep your JavaScript code in an external ".js" file.

Therefor, using xmlhttp.open("GET","yourxmlfile.xml",false) will always search for the XML file RELATIVE to the HTML document, even if the code is in an external JavaScript file.

If the responseText returns null, the local file could not be found in the specified path location. And if async is set to true, the file will be created and the response text will carry the contents of an empty XML document.

Example:

xmlDoc = xmlhttp.responseText; //String data type

        or

xmlDoc = xmlhttp.responseXML; //XML data type

  • folder1 = folder2, index.html.
  • folder2 = index.js, yourxmlfile.xml.
  • path to XML from HTML = "folder2/yourxmlfile.xml", not "yourxmlfile.xml".

And remember to parse the XML doc into "text/xml", after opening and before sending request.

Example:

  • xhttp.overrideMimeType('text/xml');
  • Chrome: xmlDoc = (new DOMParser()).parseFromString(xmlDoc,"text/xml");
1

For me it was a simple problem. There was a syntax error in my xml/php file. When I viewed the file in my browser, the browser did not detect any errors.

Make sure the elements within your XML document are properly closed!

1
  • Yes, the XML returned from "server" must be in correct format, else the XMLresponse will be null. I had double attributes on my tags, and it failed. I had an "&" in my xml, and it failed.
    – Kristian
    Oct 9, 2015 at 11:58
1

Malformed XML of any sort will cause this problem. For example, I had an attribute renderer (without ="something") which is invalid and causes DOMParser - which is called by XMLHTTPRequest - to choke. Hence load event will contain the text response but not the XML.

1

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

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.