Jump to content

how to get value from onreadystatechange function


sajan
 Share

Recommended Posts

Hi I have the following functions to get a value using ajax.

var file="xyz.php";	var str="user="+escape(user_id)+"&id="+escape(id); ret=sendRequest(file,str);    function sendRequest(file,str)	{		xmlHttp=GetXmlHttpObject();		   if (xmlHttp==false)			{			alert ("Your browser does not support AJAX!");		   return; 		  } 		xmlHttp.open("GET", file+'?'+str);		xmlHttp.onreadystatechange = handleResponse;			xmlHttp.send(null);					 }function handleResponse() 			{  					if(xmlHttp.readyState == 4)				{   if (xmlHttp.status == 200)					 				  { 				   					 var response = xmlHttp.responseText;  				   return response;				 	 }				}  							 }   function GetXmlHttpObject()	{ 	 var xmlHttp = false; 	  try {	 xmlHttp = new XMLHttpRequest(); 	  } catch (trymicrosoft) {	 try {	 	   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   	  } catch (othermicrosoft) {		   try {	   		 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");	   } catch (failed) {			 xmlHttp = false;	   }  	 }	   }	 return xmlHttp;	}

How can i get the value in response to ret?

Link to comment
Share on other sites

You need to use a global variable for the AJAX object. The first function creates the object as a local variable, it needs to be a global variable in order for the response handler to be able to use it. Declare the xmlHttp variable using the var keyword in the global scope so that both functions have access to it.

Link to comment
Share on other sites

the response variable in the function handleResponse() getting values whenever the function sendRequest() called.Here the response variable or xmlHttp.responseText in handleResponse has the value admin.I need this value to available in the ret variable so that i can carry out necessary checking.

Link to comment
Share on other sites

the response variable in the function handleResponse() getting values whenever the function sendRequest() called.Here the response variable or xmlHttp.responseText in handleResponse has the value admin.I need this value to available in the ret variable so that i can carry out necessary checking.
You cannot get the response in the return from sendRequest(). The response is not avaliable straight away as it's an asynchronous call. That's why you have to check for readystatechange--this event is there precisely to enable you get the response from the xmlHttp object once it's ready. Have a look at http://www.w3schools.com/ajax/ajax_xmlhttprequest.asp if you need an example.To respond to justssomeguy, yes you do need to use a global variable for xmlHttp, but actually it already is. Don't forget that one of the quirks of javascript is that the use of the var keyword to declare xmlHttp in function GetXmlHttpObject() makes xmlHttp global once GetXmlHttpObject() has been called.
Link to comment
Share on other sites

Don't forget that one of the quirks of javascript is that the use of the var keyword to declare xmlHttp in function GetXmlHttpObject() makes xmlHttp global once GetXmlHttpObject() has been called.
No, that's not a "quirk" of Javascript and it's not true, that's not how scoping in Javascript works. Using var inside a function specifies that the variable is local to that function only, so it overrides a global variable with the same name. Run this script and notice that func2 does not change the value of the global test var:
var test = "test1";function func1(){  test = "test2";  alert("func1: " + test);}function func2(){  var test = "test3";  alert("func2: " + test);}alert("global: " + test);func1();func2();alert("global: " + test);

Again, the xmlHttp variable is not global. If he wants his code to work he needs to make it global. He is using the AJAX object correctly by assigning a response handler to onreadystatechange and by checking the state, that's how you're supposed to do it. It's a scoping issue, the AJAX variable gets destroyed when the first function ends.

Link to comment
Share on other sites

I need this value to available in the ret variable so that i can carry out necessary checking.
Just modify your handleResponse method.
function handleResponse(){	if(xmlHttp.readyState == 4)	{		if (xmlHttp.status == 200)		{			var response = xmlHttp.responseText;			MyOtherFunctionThatDoesSomethingWithTheResponse(response);		}	}}

Link to comment
Share on other sites

Also, instead of doing this:ret=sendRequest(file,str);Make ret a global variable, then call the function:var ret;sendRequest(file,str);And in the response handler set the ret variable.

function handleResponse(){	if(xmlHttp.readyState == 4)	{		if (xmlHttp.status == 200)		{			ret = xmlHttp.responseText;		}	}}

Link to comment
Share on other sites

No, that's not a "quirk" of Javascript and it's not true, that's not how scoping in Javascript works. Using var inside a function specifies that the variable is local to that function only, so it overrides a global variable with the same name. [...]
You are quite right (and you also illustrate a separate point about masking global variables)--thanks for putting the record straight on the "quirk" I referred to however, because I was refreshing my memory on it and stated it the wrong way round. In fact it is when var is not used that a variable declared in a function has global scope once the function has been called. Try this script:
function func1(){	test = "test1 declared in func1";	alert("func1: " + test);}func1();alert("global after myfunc call: " + test);

So the xmlHttp declaration does need to change to make it global, either by removing var or preferably by declaring xmlHttp outside the function, so that it can be accessed by the event handler.

Link to comment
Share on other sites

It's probably better to leave the xmlHttp variable as a local variable in the GetXmlHttpObject function because that function returns that variable for use elsewhere. So leaving it local to that function is fine since it gets returned later. The only thing that needs to be changed is to declare the variable in the global scope, all the other code will work as-is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...