I have this code that loads an xml file using javascript:
function getXmlDocument(sFile) {
var xmlHttp, oXML;
// try to use the native XML parser
try {
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", sFile, false); // Use syncronous communication
xmlHttp.send(null);
oXML = xmlHttp.responseXML;
} catch (e) {
// can't use the native parser, use the ActiveX instead
xmlHttp = getXMLObject();
xmlHttp.async = false; // Use syncronous communication
xmlHttp.resolveExternals = false;
xmlHttp.load(sFile);
oXML = xmlHttp;
}
// return the XML document object
return oXML;
}
If the extension of the ‘sFile’ is not .xml the function returns “” always. What should I do to fix this?
I think it’s a problem on the server side: files with another extension than
.xmldon’t get the MIME type oftext/xmlor something alike and the browser(‘s XML parser) doesn’t recognize it as XML.Be sure that your content is served with the correct MIME type by your server software. With Apache, you can change this in the
.htaccessfile. Dynamically generated XML should be sent with an appropriateContent-Type:header. In PHP, you can do this with theheaderfunction.