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";
}

Thank You Very Much ...
Regards
Manikanda raj S

The problem is that the function that calls the ajax is complete after the call is made and it won't have access to the data on the ready state change. You need to put whatever you need to do with the data (ie call another function) inside the onreadystatechange function. You might think of it like calling in a pizza order that gets delivered to another house. The person calling makes the order to the pizza place but doesn't have access to the pizza after the pizza is delivered because it gets delivered to another place. (rough I know but it's the best life analogy i could think of). Basically the onreadystatechange is an event that gets assigned a handler. The handler can be defined outside of the calling function and isn't apart of that calling function's actions. It's the same concept of assigning click event (or other event) handlers inside of an init function. The init function won't have access to the data that is produced by the click events because the click events haven't triggered the handlers during the init's process.

Hi scrappedcola,
I am already doing what you said...
Here's the Code ...

function AJAXReuqest(url,paramstring,ctype)
{
	httpReq = GetXmlHttpRequestObject();					//new XMLHttpRequest();
	var params = paramstring;
	contentType = ctype;
	httpReq.onreadystatechange = AjaxResult;
	httpReq.open("POST", url, true);
	httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpReq.send(params);

}

function AjaxResult() //Call a function when the state changes.
{
	if(httpReq.readyState == 4)
	{
		if(httpReq.status == 200)
		{
			if(contentType=="XML")
			{
				obj=httpReq.responseXML;
			}
			if (contentType=="JSON" || contentType=="TEXT")
			{
				obj=httpReq.responseText;
			}
			var click=1;
			mani(obj,click);
		}
	}
}
var clickcount=0;
function mani(obj,click)
{
	var url="getxml.php";
	var paramstring="abc=200&def=2";
	var ctype="XML";
	clickcount +=1;
	// URL - URL of PHP, paramstring - Parameters to Send , Ctype-ContentType (Strictly XML,JSON,or TEXT)
	if(click==0)
	{
//	alert("Click=0");
	AJAXReuqest(url,paramstring,ctype);
	}
	if(click==1)
	{
	alert("Click=1"+obj);
	typeobj=""+obj+"";
	if(typeobj=="[object XMLDocument]")
	{
//	alert("XML");
	colarray=["No","SGClicks"];
	DrawTable(obj,colarray);
	return;
	}
	}
}
function thk()
{
click=0;
obj=new Object();
mani(obj,click);
}

I thought there may be a way to access the AJAX Response out of httponreadystatechange function , which i may not know ...
Thanks for the reply...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.