473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1167
acoder
16,027 Expert Mod 8TB
The code dealing with the response should be in the onreadystatechange function.
Nov 27 '07 #2

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

Similar topics

4
by: bobzimuta | last post by:
I'm creating a simple AJAX library. It's an object that will return an array containing the response text or xml. I'm trying to find a way to assign the response as a property of the object, but...
31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
5
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't solve: With AJAX you typically update/replace only...
3
by: Alok yadav | last post by:
I have an open IP and on that IP our main application is hosted. it uses ajax. in web.config file i have register ajax handlers. there are also other sites or project on that IP. now my problem is...
3
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
3
by: willl69 | last post by:
Hi All, Ive been having a problem of late with one of my sites that uses PHP5 / Ajax. The problem is that periodically the ajax functions lock up and it gets stuck in the loading phase of the...
2
by: majidtou | last post by:
Hi , We have an HTML page which is using AJAX. We make an AJAX call which retrieves a table with mutiple rows. We need to extend this functionality. In the table, we want to put a button on each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.