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

XMLhttp object goes away

P: 1
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
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,390
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

Post your reply

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