2

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
  • try this : xmlhttp.open("GET", langadr,true);xmlhttp.send(null); Commented Feb 15, 2012 at 14:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This, along with the suggestion in the comment above, doesn't work (the results are the same). I'll try async calls after the weekend, but meanwhile I'd like to know what's wrong here.
A local test worked fine for me. Do you get any error message? Have you considered the restrictions of the same origin policy for ajax request?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.