[英]Load local xml file using javascript in Google Chrome
我認為在 Google Chrome 的 v5 之前,下面的代碼可以正常工作。 現在在最新版本中,我在本地打開網頁時收到以下錯誤:
“XMLHttpRequest 無法加載 file:///C:/Temp/Course.xml。跨源請求僅支持 HTTP。”
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;
}
// get the best ActiveX object that can read XML
function getXMLObject() {
// create an array with the XML ActiveX versions
var aVersions = new Array("Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0");
// loop through the array until we can create an activeX control
for (var i=0; i<aVersions.length; i++) {
// return when we can create the activeX control
try {
var oXML = new ActiveXObject(aVersions[i]);
return oXML;
}
catch(e) {
}
}
// could not create an activeX, return a null
return null;
}
我真的不想每次都被迫從 web 服務器打開 web 頁面。
出於安全原因,默認情況下禁用本地文件訪問。 嘗試使用參數--allow-file-access從命令行啟動 Google Chrome
如果您只是啟動本地網絡服務器並從 localhost 獲取 html和xml 會更安全。
您可以輕松地避免部署文件,只需讓服務器為您放置 xml 的本地文件夾的內容提供服務。
這樣你避免
服務器到 go 是一個易於安裝的網絡服務器的示例 http://www.server2go-web.de/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.