I have js code like this :
function getData(){
var url = "http://www.server.loc/?get=data"; /* resource */
var xmlhttp = new XMLHttpRequest(); /* call XMLHttpRequest init */
xmlhttp.open("GET", url,true);
if(xmlhttp){
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){ if(xmlhttp.status==200){
/* success, then clear console */
return xmlhttp.responseText; /* return value is 'available' */
} else {
return "null";
} }
}
}
xmlhttp.send(null);
}
/* SHOW RESULT */
var result = getData();
console.log('result: '+result); /* result must "null" or "available" */
In web console print just show Result : undefined
. How could that be? where is the code wrong?