hardmax
hardmax

Reputation: 435

Problems with XML HTTP request in Javascript

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?

Upvotes: 2

Views: 1857

Answers (1)

Sirko
Sirko

Reputation: 74086

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.

Upvotes: 1

Related Questions