0

Basicly, i've used

<script>
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "db.xml", false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;

    document.write("<table border='1'>");
    var x = xmlDoc.getElementsByTagName("item");
    for (i = 0; i < x.length; i++)
    {
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("desc")[0].childNodes[0].nodeValue);
        document.write("</td></tr>");
    }
    document.write("</table>");
</script>

From w3school, to do something in my mind that is simple, and should work. But im suspecting that it needs to be on a server for it to work, either that or i can't use it properly. For the XML i've got several

<item>
<title>example</title>
<desc>description</desc>
<tags>Drama, Fantasy, Romance</tags>
<date>01 08 1982</date>
<img>img/nna.jpg</img>
<a_link>Link</a_link>
</item>

Basicly, my question is. Does this work on my own computer, i'm suspecting that it won't let me due to some security setting and that i need to use json. Please enlighten me.

4

1 回答 1

1

Your code works for me on a server, but does not work just loading the same HTML page in the browser.

It won't work locally because .open is getting the xml file from the server (see W3 Schools description). When you run it locally there is nothing to serve the xml file.

于 2013-10-04T07:52:11.253 回答