Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - responseXML always null

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...