0

So when I run the following code in w3schools live code test feature, the output is the same as seen in the code snippet (4) but when I run it with files that I have locally stored, the output is just 'test' instead of 4. Any ideas why or how to fix?

<!DOCTYPE html>
<html>
<body>

<p id="demo">test</p>

<script>
var count = 0;
var xhr = new XMLHttpRequest();  
xhr.open( 'GET', 'https://dl.dropbox.com/s/bsb8g518r23y0wf/books.xml?dl=0', true );  
xhr.onreadystatechange = function ( e ) {  
    if ( xhr.readyState == 4 && xhr.status == 200 )
        document.getElementById("demo").innerHTML = xhr.responseXML.getElementsByTagName( "title" ).length ;
};
xhr.send( null );  

//document.getElementById("demo").innerHTML = xhr;
</script>

</body>
</html>

In w3schools code testing and local files, it is 'books.xml' instead of the dl.dropbox link, same in the local files which refers to a books.xml file that I have stored in the same file as the HTML and js

xhr.open( 'GET', 'books.xml', true );
JooHyun
  • 37
  • 1
  • 4
  • Take a look at the JavaScript console in your browser. I suspect it's giving you an error about trying to use AJAX across domains and/or protocols. – David Dec 06 '16 at 18:58
  • Oohh it is giving me the following error in the console: "test.html:15 XMLHttpRequest cannot load file:///C:/ ... /Test_code/books.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.(anonymous function) @ test.html:15" – JooHyun Dec 06 '16 at 19:02

0 Answers0