在 Google Chrome 中使用 javascript 加载本地 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6225371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:58:31  来源:igfitidea点击:

Load local xml file using javascript in Google Chrome

javascriptxmlgoogle-chrome

提问by Cornel

I think until v5 of Google Chrome the below code worked. Now in the latest version I get the following error when opening my webpage locally:

我认为直到谷歌浏览器的 v5 下面的代码才有效。现在在最新版本中,在本地打开我的网页时出现以下错误:

"XMLHttpRequest cannot load file:///C:/Temp/Course.xml. Cross origin requests are only supported for HTTP."

“XMLHttpRequest 无法加载 file:///C:/Temp/Course.xml。只有 HTTP 支持跨源请求。”

The Javascript code:

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;
}

I really don't want to be forced to open the web page from a web server every time.

我真的不想每次都被迫从 Web 服务器打开网页。

回答by gregers

Local file access is disabled by default for security reasons. Try starting Google Chrome from the command line with the argument --allow-file-access

出于安全原因,默认情况下禁用本地文件访问。尝试使用参数--allow-file-access从命令行启动 Google Chrome

回答by rdmueller

It would be more secure if you just start a local webserver and fetch your html andxml from localhost.

如果您只是启动本地网络服务器并从本地主机获取 htmlxml ,则会更安全。

You can easily avoid deploying of the files by just let the server serve the contents of a local folder in which you place your xml.

您可以轻松地避免部署文件,只需让服务器为您放置 xml 的本地文件夹的内容提供服务。

This way you avoid

这样你就可以避免

  • having to start chrome in an unsecure mode
  • having problems when you later deploy your app to a server on the internet
  • 必须在不安全模式下启动 Chrome
  • 稍后将应用程序部署到 Internet 上的服务器时遇到问题

server to go is an example for an easy to install webserver http://www.server2go-web.de/

server to go 是一个易于安装的网络服务器的例子http://www.server2go-web.de/