1

I have to display a XML data in a table in a HTML file.

I try turning off all my extensions on FireFox but I still get a blank screen.

Employees Details

<script>
        var client;
        if (window.XMLHttpRequest) {
            client = new XMLHttpRequest();
        }
        else {
            client = new ActiveXObject("Microsoft.XMLHTTP");
        }
        client.open('GET', 'employees.xml', false);

        client.onreadystatechange = function() { // will run when the file loads
            // get the response XML
            var xmlDoc = client.responseXML;

            // get the list of user
            var user = xmlDoc.getElementsByTagName("user");

            // get the container where you want to embed the table
            var container = document.getElementById("container");

            var tableString = "<table border='1'>"; // Make a table and put the element data inside it
            for (i = 0; i < user.length; i++) {
                tableString += "<tr><td>";
                tableString += user[i].getElementsByTagName("Firstname")[0].childNodes[0].nodeValue;
                tableString +="</td><td>";
                tableString += user[i].getElementsByTagName("Phone")[0].childNodes[0].nodeValue;
                tableString += "</td></tr>";
            }
            tableString += "</table>";

            // append the table to the container
            container.innerHTML = tableString;
        }

        client.send();

    </script>
<body>
    <h1></h1>
    <div id="container"></div>
</body>

    <?xml version = "1.0" encoding ="UTF-16"?>
    <user_information>
       <user id="1">
          <Firstname> Bobby </Firstname>
          <Lastname>Drake</Lastname>
          <major>Criminal Justice</major>
          <location>Illinois</location>
          <Phone>(923)949-2302</Phone>
       </user>

       <user>
          <Firstname>Scott</Firstname>
          <Lastname>Summers</Lastname>
          <major> </major>
          <location> California </location>
          <Phone>(395)984-7284</Phone>
       </user>

       <user>
          <Firstname>Jean</Firstname>
          <Lastname>Grey</Lastname>
          <major> </major>
          <location>New York</location>
          <Phone>(843)759-6943</Phone>
       </user>

       <user>
          <Firstname>Kevin</Firstname>
          <Lastname>Sydney</Lastname>
          <major> </major>
          <location>New York</location>
          <Phone>(571)-089-4568</Phone>
       </user>

            <user>
              <Firstname>Suzanne</Firstname>
              <Lastname>Chan</Lastname>
              <major> </major>
              <location>Illinois</location>
              <Phone>(685)-583-2849</Phone>
           </user>

</user_information>

I get a blank screen when I open the html in FireFox. I don't know if it is my code or something on my computer. I would really appreciate it if someone could help me out.

1 Answers1

0

I assume you are trying to access these files using file:/// or from a local directory which is causing CORS policy violation and your request is blocked.

You can either use a web-server to deploy the resource and access it. Or check related thread comment which can help you to run Chrome with expected CORS policy.

Cross origin requests are only supported for HTTP but it's not cross-domain

www.hybriscx.com
  • 1,129
  • 4
  • 22