No se puede analizar el archivo xml local con javascript

When i try to parse local xml file using simple javascript, the code after xhr.open is not getting executed.

<html>
<head>
<script type="text/javascript">
function createXMLHttpRequestObject()
{
    if (window.XMLHttpRequest) {
      xhttp=new XMLHttpRequest();
    } else {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    return xhttp;
}

function makeRequest()
{
    var xhr = createXMLHttpRequestObject();

    try {
    xhr.open("GET", 'books.xml', false);
    xhr.send(null);
    //xhr.onreadystatechange = function() {
     //if(this.readyState == 4) {
      xmlDoc=xhr.responseXML;
     alert(xmlDoc);
     //}
   }
   } catch( err ) {
        alert("ERROR: " + err.description);
    }
}
</script>
</head>
<body>
    <div onclick="makeRequest();">test<br></div>
    <div id="out">Output Here</div>
</body>
</html>

the books.xml file is as below,

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->

<bookstore>

    <book category="COOKING">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <price>30.00</price>
    </book>

    <book category="CHILDREN">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>

    <book category="WEB">
      <title lang="en">XQuery Kick Start</title>
      <author>James McGovern</author>
      <author>Per Bothner</author>
      <author>Kurt Cagle</author>
      <author>James Linn</author>
      <author>Vaidyanathan Nagarajan</author>
      <year>2003</year>
      <price>49.99</price>
    </book>

    <book category="WEB">
      <title lang="en">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
    </book>

</bookstore>

When searched on internet i found that "For security reasons, modern browsers do not allow access across domains." http://www.w3schools.com/xml/xml_parser.asp

Por favor, ayuda.

preguntado el 08 de noviembre de 11 a las 11:11

3 Respuestas

Local files are best considered a no-go area. Security restrictions are pretty high. Run a web server on localhost instead and use that to access your page.

respondido 08 nov., 11:15

you may try to use a local web server to resolve the problem quickly.

respondido 08 nov., 11:15

Instead of setting web server on localhost....simply download aptana studio software and import your project in it...and run your html file, aptana directly open browser and automatically create web server with name http://127.0.0.1:8000/yourprojectName

After that you will not receive any error.

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.