您好我是社区新手。我想问一个问题。
我正在尝试创建用于加载和进行测验的模板HTML5。我有一个带有问题和答案的xml文件,我正在尝试加载它,在我的模板中。
我使用的代码是:
加载xml文件
// The Script that loads the XML File Locally only works in Firefox for now
function loadXMLDoc(XMLname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", XMLname, false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(XMLname);
return xmlDoc;
}
else {
xmlhttp = new XMLHttpRequest();
//Open the file using the GET routine
xmlhttp.open("GET", XMLname, false);
//Send request
xmlhttp.send(null);
//xmlDoc holds the document information now
return xmlDoc.responseXML;
}
alert("Error loading document!");
return null;
}
将内容传递给我的HTML5模板
xmlDoc=loadXMLDoc("test"+file+".qxml");
我的问题是没有检索xmlfile中的数据。在服务器或任何其他浏览器上,xmlDoc变量显示为null。
你可以指点我的方向,因为我是Javascript xmlhttprequest方法的新手。非常感谢你的时间。
文件扩展名不是xml(它是.qxml)。问题是文件.qxml的扩展名。那么有没有办法绕过这个并使用我的扩展qxml而不是xml?
答案 0 :(得分:1)
尝试服务器返回override the mime type,并告诉浏览器数据是XML。
// The Script that loads the XML File Locally only works in Firefox for now
function loadXMLDoc(XMLname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", XMLname, false);
xmlDoc.overrideMimeType('text/xml');
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(XMLname);
return xmlDoc;
}
else {
xmlhttp = new XMLHttpRequest();
//Open the file using the GET routine
xmlhttp.open("GET", XMLname, false);
xmlhttp.overrideMimeType('text/xml');
//Send request
xmlhttp.send(null);
//xmlDoc holds the document information now
return xmlDoc.responseXML;
}
alert("Error loading document!");
return null;
}