【问题标题】:Why is this working in fireFox but not in Chrome or Internet Explorer 9?为什么这在 fireFox 中有效,但在 Chrome 或 Internet Explorer 9 中无效?
【发布时间】:2015-10-14 23:14:35
【问题描述】:

我有一个 xml 文件和一个引用它的文件。在 Firefox 中,我得到了预期的响应,但在我安装的其他两个浏览器(Chrome 和 IE9)中没有。 谁能告诉我我做错了什么? HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Test File List Document</title>
    </head>

    <body>
        <div>Output</div>
        <div id="testDiv"></div>
    </body>
    <script>
        //XML request
        var xmlhttp, xmlDoc;
        // code for IE6, IE5
        if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        };
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "shredder-data.xml", false);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;

        //test
        document.getElementById("testDiv").innerHTML=xmlDoc.getElementsByTagName("crc")[0].childNodes[0].nodeValue;
    </script>
</html>

XML (shredder-data.html}

<?xml version="1.0" encoding="utf-8"?>
<shredders>
    <personal>
        <model name="P-20">
            <headline>Fellowes PowerShred P-20 Strip-Cut Personal Shredder</headline>
            <users>1</users>
            <crc>3401401</crc>
            <cut>Strip</cut>
            <jam>no</jam>
            <sheet_capacity>5</sheet_capacity>
            <run_time>2/25</run_time>
            <bin_capacity>11</bin_capacity>
            <staples>no</staples>
            <paperclips>no</paperclips>
            <credit-cards>no</credit-cards>
            <CDs>no</CDs>
        </model>
        <model name="P-35c">
            <headline>Fellowes Powershred P-35C Cross-Cut Personal Shredder with Safety Lock</headline>
            <users>1</users>
            <crc>3008801</crc>
            <cut>Strip</cut>
            <jam>no</jam>
            <sheet_capacity>2-4/15</sheet_capacity>
            <run_time>2/25</run_time>
            <bin_capacity>4.5</bin_capacity>
            <staples>yes</staples>
            <paperclips>no</paperclips>
            <credit-cards>yes</credit-cards>
            <CDs>no</CDs>
        </model>
    </personal>
</shredders>

我希望的输出是:

Output
3401401

【问题讨论】:

    标签: javascript html google-chrome internet-explorer firefox


    【解决方案1】:

    脚本中有一个 XHR 调用。默认情况下,Chrome 不会通过 XHR 从本地文件系统加载文件。如果通过 file:// 协议查看示例,这会影响资源的加载。要启用此类文件的加载,您应该使用标志 --allow-file-access-from-files。

    IE 也有一些限制,尽管在某些条件下本地访问的 XHR 调用可以工作。

    或者,从网络服务器而不是本地文件系统提供此服务。

    【讨论】:

    • 为什么你认为 OP 在file:// 协议下?在 Firefox 中工作。如果这个问题是你所说的,在 Firefox 中应该不起作用,但可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2015-05-20
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多