1

您好,我是社区的新手。我想问一个问题。

我正在尝试创建一个模板 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 变量显示为空。

你能指出我的方向吗,因为我是 Javascript xmlhttprequest 方法的新手。非常感谢您抽出宝贵时间。

文件扩展名不是 xml(它是 .qxml)。问题是文件.qxml 的扩展名。那么有什么方法可以绕过这个并使用我的扩展名 qxml 而不是 xml 吗?

4

1 回答 1

1

尝试覆盖服务器返回的 mime 类型,并告诉您的浏览器数据是 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;
}​
于 2012-11-08T10:22:27.330 回答