2012-11-08 36 views
1

您好,我是社區新手。我想問一個問題。XMLHttpRequest僅在加載xml文件時使用Firefox本地工作

我想創建一個用於加載和測驗的模板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?

+0

回答我自己的問題,問題是文件.qxml的擴展名。那麼有沒有什麼辦法繞過這個,並使用我的擴展名爲qxml而不是xml?提前致謝。 – Hellykun

回答

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; 
}​ 
+0

這對你的寶貴幫助非常感謝!我可以的話+1 – Hellykun

相關問題