Reputation: 2785
I want to retrieve an XML from a URL and store it in a variable xmlDoc
.
I have the following:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","localhost:8080/rest/xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
But I am not getting the XML file, is there something I need to add?
Upvotes: 0
Views: 1961
Reputation: 342
before sending, attach a callback like this.
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseText;
}else if (xmlhttp.readyState==4 && xmlhttp.status != 200)
{
alert("error-" + xmlhttp.responseText);
}
}
Upvotes: 0
Reputation: 18783
The open
method is passing false
as the last parameter, making this a synchronous request. The OP's original code is correct, except for one thing: the URL.
xmlhttp.open("GET","http://localhost:8080/rest/xml",false);
Or if you want to make the URL agnostic to the protocol of the current page:
xmlhttp.open("GET","//localhost:8080/rest/xml",false);
Upvotes: 2