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
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 Comments
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 Comments
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 Comments
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 Comments
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");
Comments
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 Comment
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.
Comments
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
Comments
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>