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.

asked Sep 23, 2010 at 18:25

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");
hoss
2,4683 gold badges29 silver badges43 bronze badges
answered Sep 23, 2010 at 18:33
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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 ).
This has just cropped up in Chrome (32.0.etc.etc) which was working fine previously. Cheers for the fix
And this problem is still happening in 2016, but thank you, this method fixed my page!
Useful for nowadays. The samples that I saw using reponseXML straitffarward seems to belong in ancient days of web.
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.

answered Sep 23, 2010 at 18:31

6 Comments

So, i guess i have to use regex to fetch the dom elements and its property? This is gonna hurt.
Since you are using privlidged code in Firefox, check out this page: developer.mozilla.org/en/Code_snippets/HTML_to_DOM
Their solutions for parsing an entire html document to dom are not working for me. I'm using greasemonkey, it should have some influence in this. Anyway, their solutions are complex, i might try them again. I better start digging regex.
Unfortunately, overrideMimeType can't be done in vba. :(
|
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...

answered Sep 23, 2010 at 18:27

2 Comments

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.
You're right. There are some workarounds for this, i may check them later.
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.

answered Sep 23, 2010 at 23:35

2 Comments

I have no access at all to the server, this is supposed to be a greasemonkey script.
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!
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");
answered May 24, 2013 at 2:10

Comments

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!

answered Sep 23, 2013 at 1:08

1 Comment

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.
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.

answered Apr 12, 2016 at 13:28

Comments

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

answered Nov 22, 2020 at 10:59

Comments

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>
answered Dec 16, 2018 at 20:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.