473,386 Members | 1,621 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,386 software developers and data experts.

XMLhttp object goes away

Ok, I'm very new to AJAX programming, and fairly new to Javascript. (I was originally trained on low-level C programming.) I'm trying to build a simple AJAX routine in a file named ajax.js:

Expand|Select|Wrap|Line Numbers
  1. var xmlReqs = new Array();
  2. function CXMLHttp(cb,data,xmlHttp)
  3. {
  4.     this.cb = cb;
  5.     this.data = data;
  6.     this.xmlhttp = xmlHttp;
  7. }
  8.  
  9. function ajaxConnect(page,cb,data)
  10. {
  11.     var xmlHttp;
  12.  
  13.     try
  14.     {
  15.         // Firefox, Opera 8.0+, Safari
  16.         xmlHttp=new XMLHttpRequest();
  17.     }
  18.     catch (e)
  19.     {
  20.         // Internet Explorer
  21.         try
  22.         {
  23.             xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  24.         }
  25.         catch (e)
  26.         {
  27.             try
  28.             {
  29.                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  30.             }
  31.             catch (e)
  32.             {
  33.                 alert("Your browser does not support AJAX!");
  34.                 return false;
  35.             }
  36.         }
  37.     }
  38.     xmlHttp.onreadystatechange=function()
  39.     {
  40.         for (var i=0; i < xmlReqs.length; i++)
  41.         {
  42.             if (xmlReqs[i].xmlhttp.readyState == 4)
  43.             {
  44.                 alert('got ' + xmlReqs[i].xmlhttp.responseText);
  45.                 xmlReqs[i].cb(xmlReqs[i].data, xmlReqs[i].xmlhttp.responseText);
  46.                 xmlReqs.splice(i, 1); i--;
  47.             }
  48.         }
  49.     }
  50.     var i = xmlReqs.push(new CXMLHttp(cb,data,xmlHttp));
  51.     i--;
  52.     xmlReqs[i].xmlhttp.open("GET", page, true);
  53.     xmlReqs[i].xmlhttp.send(null);
  54. }
  55.  
I have an onclick routine that calls this like this:
ajaxConnect('get_item.php?ajax=1&item=' + item, onItem, tem);

The trouble is when it hits the alert, another debugging session shows that the xmlhttp property seems to be empty. I've been staring at this for a day now but I don't see the problem. I know I'm doing some newbie error, but I don't yet see it. Any help?
Mar 25 '09 #1
1 1478
gits
5,390 Expert Mod 4TB
in your onreadystate-handler you loop through a number of request-objects in a callback for every single request ... i think this will cause the behaviour that you encounter ...

kind regards
Mar 27 '09 #2

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

Similar topics

2
by: Graham Mattingley | last post by:
Hello I had this idea that I thought was going to be really simple, and I just cant get it to work.. I have use an affiliate link on my site, and I did not like it because it did not have my...
5
by: Justice | last post by:
Currently I'm doing some experimenting with the XMLHTTP object in Javascript. Now, the XMLHttp object is asynchronous (at least in this case), and the following code causes a significant memory...
6
by: Krzysztof Kubiak | last post by:
I'm trying to make use of XMLHTTP object, but I've come across a problem. It seems that there is no way to create dynamic array of such XMLHTTP objects (to make several requests) and handle them...
3
by: kajol | last post by:
Hi everyone I am trying to get the content of any webpage (URL) using XMLHTTP, and it is working fine for me, but suddenly I have got a URL "http://www.bizrate.com/" which is causing a system...
2
by: Jason Morehouse | last post by:
Hello, Anyone know if it's possible to speak with the server via xmlhttp.open while the browser is doing a post -- file upload in this case: <form enctype="multipart/form-data"...
12
by: Botan Guner | last post by:
Hi all, Here is the problem, i'm using Microsoft.XMLHTTP for ie and XMLHttpRequest for mozilla, on my local server which is win2000 server i've no problem with that but when i uploaded the file...
3
by: Dag Sunde | last post by:
I'm trying to call an ASP script with the XMLHttp object in Client-side Javascript (which goes well). My problem is that the server gives me an error when I try to set a Session variable in the...
5
by: jim.frantzen | last post by:
You have an active XMLHTTP request on the main page (localhost/App1/index.aspx) The XMLHTTP request takes about 60 seconds to receive a response back from localhost/App1/getxml.aspx. You have an...
6
by: Vanessa | last post by:
I have a question regarding async mode for calling Microsoft.XMLHTTP object. Microsoft.XMLHTTP hangs the IE once in a while suddenly, but it will work again after half an hour or so without doing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.