PDA

View Full Version : AJAX - can't read XML from ASP



guy.murray
02-13-2007, 12:53 PM
Hi,
have just started learning AJAX and have come up against the following problem.

The following code works fine when using a plain .xml file as the input, but when substituting a .asp file that produces identical output, it doesn't recognise xmlDoc as an XML document. Curiously, xmlHttp.responseText reproduces the XML perfectly. Can anyone help?

Many thanks, Guy

<script>
var xmlHttp;
function startRequest() {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "simple.xml", true);
xmlHttp.send(null);
}
catch(err) {
alert("startRequest error: " + err.message);
}
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
try {
alert(xmlHttp.getAllResponseHeaders());
var xmlDoc = xmlHttp.responseXML;
var myTag = xmlDoc.getElementsByTagName("item1");
alert(myTag[0].firstChild.nodeValue);
}
catch(err) {
alert("handleStateChange error: " + err.message);
}
}
}
}
</script>

guy.murray
02-13-2007, 04:11 PM
Sorted it. I just needed to put

<&#37;
response.ContentType = "text/xml"
%>

in my asp file. Otherwise the response header is set to Content-type: "text/html" which I guess is what the problem was.