Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to get AJAX Response outside the http.onreadystatechange function

manisvembumanisvembu Posts: 1Member
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
[code]http.onreadystatechange =function()
{
if(http.readyState == 4 && http.status == 200)
{
obj=http.responseXML;
}
}[/code]
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 ...

[code]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 ...
[/code]


[code]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";
}[/code]

How can i get this to Work ...
Manikanda raj S
Sign In or Register to comment.