I'd like to read an XML file from on my local server from my website. This is how I'm doing it:
var xmlhttp = new XMLHttpRequest();
var langadr = "http://" + document.location.hostname + ":" + document.location.port + "/languages/language.xml";
xmlhttp.open("GET", langadr);
xmlhttp.send();
var xmlDoc = xmlhttp.responseXML;
But when I run it, I'm getting DOMException in the status and statusText fields of xmlhttp. The file is available directly via the url. The file is the sample from here. What am I doing wrong here?
1 Answer 1
You are assuming a synchronous XmlHTTpRequest, but don't set the parameter for it:
xmlhttp.open("GET", langadr, false );
Per default browsers use asynchrounous calls, which leads to your code breaking.
However, you should rewrite your code to use an asynchronous call by providing a callback. For details have a look at Using XmlHttpRequest @ MDN.
xmlhttp.open("GET", langadr,true);xmlhttp.send(null);