简体   繁体   中英

Why is this working in fireFox but not in Chrome or Internet Explorer 9?

I have an xml file and a file which is referencing it. In Firefox, I get the response I'm expecting, but not in the other two browsers I have installed (Chrome & IE9). Can anyone tell me what I'm doing wrong please? 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>

The output I was hoping for would be:

Output
3401401

You have an XHR call in the script. Chrome does not load files from the local filesystem via XHR by default. This affects the loading of resources if examples are viewed via the file:// protocol. To enable the loading of such files you should use the flag --allow-file-access-from-files.

IE has some limitations, as well, though there are conditions under which locally accessed XHR calls can be made to work.

Alternatively, serve this from a web server, rather than the local filesystem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM