I have this script that fetches information form a XML file. It works fine in W3schools Tryit Editor but not when I have it locally run on my pc. (When run on the W3 editor, you need to change questions.xml to books.xml and qtext to title.)
I copied books.xml from w3schools to test if my .xml file was bad but the script didn't work locally with books.xml either.
I have found people having similar problems but it was always due to them referencing to something in a different domain which I am aware isn't supposed work but my problem shouldn't have anything to do with that. I have tried googling further but it's hard to get google to find anything close to the search terms.
Here's the code:
<p id="xmltesti">asd</p>
<script>
var xhttp = new XMLHttpRequest(); //Create variable for XMLHttpRequest
xhttp.onreadystatechange = function() { //When readyState property changes, run function
if (this.readyState == 4 && this.status == 200) { //If everything is OK
getText(this); //Run function GetData ("this" refers to the parent object)
console.log("Everything OK!");
} else {
console.log("Problems occurred!");
console.log(xhttp.status);
}
};
xhttp.open("GET", "questions.xml", true);
xhttp.send();
function getText(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("xmltesti").innerHTML =
xmlDoc.getElementsByTagName("qtext")[0].childNodes[0].nodeValue;
}
</script>
The browser console looks like this (I am also wondering why it prints it twice):
Problems occurred!
0
Problems occurred!
0
Edit: I uploaded the files to my web server and it is now working. Problem partly solved. But it is easier to test it on my local machine than constantly having to upload files to the server. And I'd also like to know why it doesn't want to upload when I run it on my local machine.