469,306 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

complicated ajax problem

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
1 1095
acoder
16,027 Expert Mod 8TB
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.

Similar topics

4 posts views Thread by bobzimuta | last post: by
31 posts views Thread by Tony | last post: by
5 posts views Thread by Martin | last post: by
3 posts views Thread by Alok yadav | last post: by
6 posts views Thread by =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post: by
3 posts views Thread by willl69 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.