By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

complicated ajax problem

P: 1
I've been having this problem. I'll try my best to describe it as clear as possible.
here is my code snippet.

Expand|Select|Wrap|Line Numbers
  1. function AjaxAgent(){
  2. var xhr = null;
  3. var isReadyState = true;
  4. //CODE SNIPPED
  5.  
  6.     this.postRequest = function(){
  7. // CODE SNIPPED
  8.         xhr = getHTTPObject();//get the HTTPObject
  9.         if(xhr){
  10.             xhr.onreadystatechange = function(){
  11.                 if(xhr.readyState == 4){
  12.                     isReadyState = true;
  13.                     if(xhr.status != 200 && xhr.status != 304){
  14.                         alert("ERROR");
  15.                     }
  16.                 }
  17.             };
  18.             xhr.open("POST", URL, true);
  19.             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  20.             xhr.send(queryStr);
  21.         }
  22.     };
  23.  
  24.  
  25.     this.getXMLElemSize = function(elemName,httpObj){
  26.         if(httpObj==null)httpObj=xhr;
  27.         //alert(isReadyState)
  28.         if(!isReadyState){ 
  29.             return myself.getXMLElemSize(elemName,httpObj);
  30.         }else{
  31.             if(XMLResp == null) XMLResp = xhr.responseXML;
  32.             return XMLResp.getElementsByTagName(elemName).length;
  33.         }
  34.     };
  35. }

I'm writing a somesort of a common ajax class. For e.g.

Expand|Select|Wrap|Line Numbers
  1. var aa = new AjaxAgent();
  2. ...
  3. ...
  4. aa.postRequest();
  5. alert(aa.getXMLElemSize("Name"));
the problem is the request to server is done asynchronously. and by the time the code excutes aa.getXMLElemSize("Name"), the server hasn't responded yet..

and that is why in the method getXMLElemSize i try to call the function recursively.. till XMLHttp object readystate is 4. But the problem is I'll hit "Stack Overflow at line:xx " error.

The funny thing is when I put an alert message in the method getXMLElemSize(say line 27 in the 1st code snippet), everything works out just fine.

I've been scratching my head since the last 2days just to solve this problem.

You guys have any idea? Any workaround for getting asynchronous response?
Nov 27 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
The code dealing with the response should be in the onreadystatechange function.
Nov 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.