How to Get the AJAX Response 'return' to a function

Hi …
I Need to work with more AJAX Content, So i want a JS Function with Arguments like URL(the url to Ping), Parameters to Send , ctype(Content Type XML,JSON or TEXT). All i need is when i Call the Function with these Arguments , the Function should return the XML or JSON object back… But the Obj is inaccesible outside the

http.onreadystatechange =function() 
{
 if(http.readyState == 4 && http.status == 200)
 {
 obj=http.responseXML;
 }
}

i need to access ‘obj’ here outside the http.onreadystatechange =function(), SO that i can return it to the Called Function …
Now where am i Missing … or is there any Alternative ways to do this …

var url="getxml.php";
var paramstring="abc=20&def=2";
var ctype="XML";
// URL - URL of PHP, paramstring - Parameters to Send , Ctype-ContentType (Strictly XML,JSON,or TEXT) 
var xmlobj = GetTHTTPObject(url,paramstring,ctype);
alert(xmlobj); // I need to get the obj returned by the AJAX here ...

function GetTHTTPObject(url,paramstring,ctype)
{
		var http = GetXmlHttpRequestObject();					//new XMLHttpRequest();
		var params = paramstring;
//		var obj;
		http.open("POST", url, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
//		if(http.readyState == 4 && http.status == 200)
//		alert(http.responseXML);
		data=http.onreadystatechange =function() //Call a function when the state changes.
		{
			if(http.readyState == 4 && http.status == 200)
			{
			obj=http.responseXML;
			}
			alert(obj);
			return obj;
		}
		http.send(params);
		alert(obj);  // How to get the 'obj' accessible here ?
                return "obj";

How can i solve this ...
Manikanda raj S

Its not that easy … If u return the ‘obj’ inside the onreadystatechange =function()
the Value will not be returned to the calling function… I want the Value returned to the Function which Called this Function

Mani S

Can’t you just change your function to:


if(http.readyState == 4 && http.status == 200)
 {
 return obj=http.responseXML;
 }

…which should return that object to the calling function.