...

View Full Version : AJAX returning response to a function.



MikoLone
05-08-2006, 10:52 PM
Here is a question that I have that you all may have an answer but I just can't figure it out. I have just worked around it but I am finally tired of that and want to see if there is a real answer out there.

Here it is.

I have a function that calls a function that sends an AJAX request to the server. When the response comes back, I want it returned to the original function so that I can handle the response.

Here is some sudo codish stuff



function soSomething(){


//do stuff


response = doAJAXCall();

alert(response);//alerts "UNDEFINED"

}

function doAJAXCall(){
req = getXMLHTTPRequest();//makes the proper http request

req.onreadystatechange = function () {
if (req.readyState == 4 ) {
returnXML = req.responseXML;
return returnXML;
}
}

req.open("POST", 'some.php.file', true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(null);
}


the alert(response) line gives me an undefined. But if I alert returnXML instead of the return returnXML in the doAJAXCall function then I get a xml object.

I always seem to be wanting to call an AJAX call and get the response but I can't do it. I have hacked through this by calling the same function that called the ajax function when the response comes in but that seems sloppy.

I thought it was a problem with the line req.open("POST", 'some.php', true); so I changed it to req.open("POST", 'some.php', false); and it worked exactly the same.

I am using firefox (I haven't tested in IE yet) and I am not sure if that makes a difference.

Thank you in advance for any suggestions that you may have.

A1ien51
05-09-2006, 01:45 PM
That is not going to happen. You are not dealing with a normal inline executing funciton you always have been working with.

You owuld have to do an synchronous request with the XHR object which defeats the whole purpose of using it in my eyes.

Eric

MikoLone
05-09-2006, 04:58 PM
I see. So I guess either you don't have this problem or you have just learned to deal with it in a different way.

What do you do?

Basscyst
05-10-2006, 10:13 PM
I use a call back function and pass that when I make the ajax call. It uses eval, which is generally not good practice, but I find it's usefull in this case.



function getReqObjPost(url,params,func)
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
if(new ActiveXObject("Microsoft.XMLHTTP"))
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}

xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
var req_str=xmlhttp.responseText;
var req=xmlhttp.responseXML;
eval(func);
}
else
{
noData(xmlhttp.status);
}
}
}
var now=new Date();
params=params + "&c_date"+now.getSeconds()+"=" + now;
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send(params);
}


Basscyst



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum