Solved

Ajax : Multiple calls..

Posted on 2006-11-05
9
190 Views
Last Modified: 2008-01-09
Hi all,
I have a page with some simple Ajax code on it, its called a number of time to populate elements on the page. The probem is that if one ajax call is running when another starts up, it cancels the first out... anyone know of a way around this?

Here is the code im using: (If I stick an alert() at the start to force a delay it works ok, but thats not really an idea solution).

function sndReq() {
   theTarget = htmltarget
myRand=parseInt(Math.random()*99999999);
   http.open('get', 'http://www.domain.com/');
   http.onreadystatechange = handleResponse;
   http.send(null);
}

function handleResponse() {
 if(http.readyState == 4 && http.status == 200){
       var response = http.responseText;

           document.getElementById(theTarget).innerHTML = response;

   }
}
0
Comment
Question by:fox_statton
9 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
The problem is that you use only ONE http object for multyple http requests.
Create multiple http Objects with different names and use each for its particular request and response.

0
 

Author Comment

by:fox_statton
Comment Utility
How, how do I create different named http objects?
0
 
LVL 17

Expert Comment

by:gops1
Comment Utility
Otherwise, store in a global variable, the number of times the call to be made. Later you can make multiple calls here

var noOfCalls=get The Number Of Calls;
var calStatus=0;

function handleResponse() {
 if(http.readyState == 4 && http.status == 200){
       var response = http.responseText;

           document.getElementById(theTarget).innerHTML = response;
           calStatus++;
           if(calStatus<noOfCalls){ sndReq(); }

   }
}
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:fox_statton
Comment Utility
Hi Zvonko,

can you tell me how the multiple named http objects would be done?

Thanks
0
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 498 total points
Comment Utility
Make the http_request a local variable.  That will mean every time you make a new request, a new instance of that local variable appears.

function sndReq() {
   var http;
   if (window.XMLHttpRequest){
      http = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
      try {
         http = new ActiveXObject('Msxml2.XMLHTTP');
      } catch (e) {      
         try {
            http = new ActiveXObject('Microsoft.XMLHTTP');
         } catch (e) {}
      }
   }
   theTarget = htmltarget
   myRand=parseInt(Math.random()*99999999);
   http.open('get', 'http://www.domain.com/');
   http.onreadystatechange = function(){handleResponse(http);};
   http.send(null);
}

function handleResponse(httpObject) {
 if(httpObject.readyState == 4 && httpObject.status == 200){
       var response = httpObject.responseText;

           document.getElementById(theTarget).innerHTML = response;

   }
}
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Show me your actual code and I can show you how to easy extend the code to be multy Object featured.

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now