Possible Duplicate:
How to return AJAX response Text?
How to return the response from an AJAX call from a function?
After whole day without solution to this. I read that this isn't possible or cant be done etc. I did go trough all of topics related, but cant find solution.
So i think my variable in code is out of scope and i cant return it, cant use it.
js file:
function getXMLHttp()
{
var xmlHttp
try
{
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
//Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!")
return false;
}
}
}
return xmlHttp;
}
function MakeRequest()
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4)
{
var myvar = new Array();
myvar=JSON.parse(xmlHttp.responseText);
return myvar;
}
}
xmlHttp.open("GET", "showimage.php", true);
xmlHttp.send();
}
So here above is ok, its returning value of php script when its finished.
And i want this myvar variable in html file. Tried like this:
window.onload = function() {
var myvar = MakeRequest();
alert (myvar);
}
It fails, returning undefined value.
Tried also making MakeRequest function as callback function, and then calling it from html like
MakeRequest(function(txt){
});
Then I got response, but can just alert it. I don't want to alert it. I need it to be stored in variable myvar because its needed later in code.