views:

72

answers:

4

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.

A: 

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

hvgotcodes
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
A: 

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

xmlhhtp.overrideMimeType('text/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.

MooGoo
So, i guess i have to use regex to fetch the dom elements and its property? This is gonna hurt.
WoF_Angel
Since you are using privlidged code in Firefox, check out this page: https://developer.mozilla.org/en/Code_snippets/HTML_to_DOM
MooGoo
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.
WoF_Angel
A: 

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(xmlString, "application/xml");
Kenny
The xmlString would be the responseText, correct?
WoF_Angel
yes , that is correct.
Kenny
From [MDC](https://developer.mozilla.org/en/DOMParser): *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
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
Apparently it's not correct, as a parse error is thrown.
WoF_Angel
A: 

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.

Mic
I have no access at all to the server, this is supposed to be a greasemonkey script.
WoF_Angel
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