I have a standard, simple AJAX query in IE (using an Msxml2.XMLHTTP ActiveX object) which requests an ASPX page in the same domain. While the responseText field contains the complete HTML, in IE the responseXML field is blank (this works just fine in Chrome).
As part of my debugging process, I stripped the ASPX page down to a bare bit of XML, and I even ran it through an XML validator to be sure I hadn’t mistyped anything. I changed the content-type of the ASPX page to “text/xml” (IE identifies the page as an XML Document in the page properties) and the encoding to “utf-8” (both at the page level and via the globalization tag in the web.config).
When my web service returns the exact same content, the AJAX code has no trouble with it. Aslo, the AJAX has no trouble reading an XML file (containing an exact duplicate of the content) within the same project. It’s only the ASPX page it can’t seem to parse.
I’ve had nothing but trouble with the UpdatePanels (ASP.NET 3.5), so I’m going with the pure javascript approach.
Any suggestions? Am I missing something?
AJAX Code
function NavigateResults(query) {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
// These are utilized for Microsoft IE browsers
try {
//alert('Boy... you should consider upgrading your browser.');
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
//alert('Whoa... no, seriously, you really need to upgrade!!!');
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
//alert("Yikes! Your browser does not support AJAX! Your inter-webs are broken! Call a 15-year old ASAP!");
window.location = url;
return false;
}
}
}
if (xmlHttp !== null) {
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
response = xmlHttp.responseXML;
var l_stringNode;
try {
l_stringNode = response.selectNodes("div")[0];
} catch (e) {
try {
l_stringNode = response.evaluate("div")[0];
} catch (e) {
l_stringNode = null;
}
}
alert("Response Text:\n\n" + xmlHttp.responseText);
alert("Response XML:\n\n" + xmlHttp.responseXML.xml);
//document.getElementById("SearchResultsDiv").innerHTML = xmlHttp.responseText;
if (l_stringNode !== null && l_stringNode.firstChild !== null) {
try {
var serializer = new XMLSerializer();
document.getElementById("SearchResultsDiv").innerHTML = serializer.serializeToString(l_stringNode);
} catch (e) {
// IE does not support serializeToString
document.getElementById("SearchResultsDiv").innerHTML = l_stringNode.xml;
}
}
}
}
var url = prompt("URL", "http://localhost:6168/Test.xml"); // + query + "&contType=xml");
xmlHttp.open("GET", url, true);
//prompt("URL", "http://localhost:4511/Service1.asmx/Autocomplete?prefix=test");
//xmlHttp.open("GET", "http://localhost:4511/Service1.asmx/Autocomplete?prefix=test", true);
xmlHttp.send(null);
}
}
Test XML (copied from Test.aspx via View Source, exactly matches the XML file and the response from the web service)
<?xml version="1.0"?>
<ul>
<li>Testacea</li>
<li>Testament</li>
<li>Testudinata</li>
</ul>
Ok, I finally figured it out. A typically ASPX page has page information at the top, followed by content, like this:
That page information actually creates a blank line at the top of the output, which results in invalid XML (the xml declaration MUST be on the first line if it’s present at all). So, there are two solutions. One, move the xml declaration onto the same line, like so:
Two, remove the xml declaration completely.
You must still set the content-type to text/xml.