0

I am quite new to javascript programming and I sending xmlhttprequest whose reply is in xml format. when I use a proxy and intercept the response, I can see the reply as expected and in xml format. however I want to retrieve the response and display the value in html or just some alert. But I am getting an error that xmlresponse is null. why is the value from xmlhttp.responseXML null. below is my code. please does anyone has an idea on what is wrong.

var xmlhttp=new XMLHttpRequest();
xmlhttp.open('GET','http://remoteserver.net/search/myvalues?format=xml','true');
xmlhttp.send();
alert(xmlhttp.responseText);
alert(xmlhttp.responseXML);
var xmlresponse=xmlhttp.responseXML; 
var latitude;
var longitude;

try{
if(xmlhttp.status==200){

   x=xmlresponse.getElementsByTagName("place");
  latitude=x.getAttribute("lat");
  longitude=x.getAttribute("lon");
  coordonates="the latitude is "+latitude+" and the longitude is "+longitude;
  //document.getElementById("output").innerHTML=coordonates;
  alert(coordonates);
 }

}
catch(err)
{
document.getElementById("output").innerHTML=err.message;
}

so far what this is what I have as information How to get the response of XMLHttpRequest? also http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp

Community
  • 1
  • 1
user4237435
  • 333
  • 1
  • 2
  • 12

1 Answers1

1

The 3rd parameter you are passing to xmlhttp.open(); is true, which means asynchronous (note that you shouldn't quote the true - you want to pass a true value, not the string "true".

This means that the function will return straight away, while the actual request happens in the background. So if you straight away check the result, the real request won't have responded yet, and you will get null as you are seeing.

You should use something like the following, before you call send():

xmlhttp.onreadystatechange = function () {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    alert(xmlhttp.responseText);
  }
}

to listen for when the AJAX request completes, then alert you when it does.

P.s. if you are new to Javascript, I would reccomend not using w3schools - it's example code is not very high quality, and use jQuery - most sites on the web use it to make a lot of stuff a lot easier. Although mentioning "use jQuery" on Stack Overflow will often lead to a flamewar.

A jQuery solution would look like:

$.get('http://remoteserver.net/search/myvalues?format=xml').then(function (data) {
  alert(data);
}, function (errror) {
  alert("An error occurred");
});
rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • thanks a lot.. it did work.. but now the question I am facing is how do get to reuse the xmlhttp instance or how do i create the variable dynamically.. to get something like xmlhttp1,xmlhttp2 etc.. for now arrays are not working.. I am getting an error "missing semicolon" for something like xmlhttp[number]. this are mysources for array and for concantenation I used window["xmlhttp"+value] but it not working either – user4237435 Dec 04 '14 at 15:36