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

multiple ajax calls simultaneously

Hey,

In the following code the function showdivs() is called on body load, though only the second div is loaded. I've been searching on the web for similar problems and I know it has to do with the handler function, which is supposed to be run twice but now just cancels the first function as soon as the second one is called. Could anybody explain me how to solve this?
TIA,

Woods

Expand|Select|Wrap|Line Numbers
  1. var time_variable;
  2.  
  3. function getXMLObject()  //XML OBJECT
  4. {
  5.    var xobject = false;
  6.    try {
  7.      xobject = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
  8.    }
  9.    catch (e) {
  10.      try {
  11.        xobject = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
  12.      }
  13.      catch (e2) {
  14.        xobject = false   // No Browser accepts the XMLHTTP Object then false
  15.      }
  16.    }
  17.    if (!xobject && typeof XMLHttpRequest != 'undefined') {
  18.      xobject = new XMLHttpRequest();        //For Mozilla, Opera Browsers
  19.    }
  20.    return xobject;  // Mandatory Statement returning the ajax object created
  21. }
  22.  
  23. var xobject = new getXMLObject();    //xmlhttp holds the ajax object
  24.  
  25.  
  26. function handler() {
  27.     var getdate = new Date();
  28.  
  29.     if(xobject) {
  30.         xobject.open("POST",phpFile,true);
  31.         xobject.onreadystatechange  = function() {
  32.             if (xobject.readyState==4) {
  33.                 if(xobject.status == 200) {
  34.                     document.getElementById(divID).innerHTML=xobject.responseText;
  35.                 }
  36.                 else {
  37.                     alert('error');
  38.                 }
  39.             }
  40.         }
  41.         xobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  42.         xobject.send(postVars);
  43.     } 
  44. }
  45.  
  46.  
  47. function showdiv1() {
  48.       divID = "div1"; //define html div
  49.       phpFile = "showdiv1.php"; //define file to be loaded
  50.       postVars = "null"; //define post vars
  51.       new handler();
  52. }
  53.  
  54. function showdiv2() {
  55.       divID = "div2"; //define html div
  56.       phpFile = "showdiv2.php"; //define file to be loaded
  57.       postVars = "null"; //define post vars
  58.       new handler();
  59. }
  60.  
  61. function showdivs() {
  62. showdiv1();
  63. showdiv2();
  64. }
  65.  
Mar 9 '11 #1
2 2250
Dormilich
8,658 Expert Mod 8TB
actually, both are loaded. it’s just that the second overwrites the first one.

PS. it’s null, not "null"
Mar 9 '11 #2
Solved it - suppose nice to share?

Expand|Select|Wrap|Line Numbers
  1. var time_variable;
  2.  
  3. function getXMLObject()  //XML OBJECT
  4. {
  5.    var gobject = false;
  6.    try {
  7.      gobject = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
  8.    }
  9.    catch (e) {
  10.      try {
  11.        gobject = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
  12.      }
  13.      catch (e2) {
  14.        gobject = false   // No Browser accepts the XMLHTTP Object then false
  15.      }
  16.    }
  17.    if (!gobject && typeof XMLHttpRequest != 'undefined') {
  18.      gobject = new XMLHttpRequest();        //For Mozilla, Opera Browsers
  19.    }
  20.    return gobject;  // Mandatory Statement returning the ajax object created
  21. }
  22.  
  23. function handler(div) {
  24.  
  25.     var xobject = new getXMLObject();    //xmlhttp holds the ajax object
  26.     var randint = Math.random();
  27.     var getdate = new Date();
  28.  
  29.         xobject.open("POST",phpFile,true);
  30.         xobject.onreadystatechange  = function() {
  31.             if (xobject.readyState==4) {
  32.                 if(xobject.status == 200) {
  33.                     document.getElementById(div).innerHTML=xobject.responseText;
  34.                 }
  35.                 else {
  36.                     alert('error');
  37.                 }
  38.             }
  39.         }
  40.         xobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  41.         xobject.send(postVars);
  42.     } 
  43.  
  44.  
  45. function showdiv1() {
  46.       divID = "div1"; //define html div
  47.       phpFile = "showdiv1.php"; //define file to be loaded
  48.       postVars = "var1=somevalue"; //define post vars
  49.       handler(divID);
  50. }
  51.  
  52. function showdiv2() {
  53.       divID = "div2"; //define html div
  54.       phpFile = "showdiv2.php"; //define file to be loaded
  55.       postVars = "var2=somevalue"; //define post vars
  56.       handler(divID);
  57. }
  58.  
  59. function showdiv3() {
  60.       divID = "div3"; //define html div
  61.       phpFile = "showdiv3.php"; //define file to be loaded
  62.       postVars = "verzend=somevalue"; //define post vars
  63.       handler(divID);
  64. }
  65.  
  66. function showdivs() {
  67. showdiv1();
  68. showdiv2();
  69. showdiv3();
  70. }
  71.  
Mar 11 '11 #3

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

Similar topics

5
by: cmercier | last post by:
Hi everyone! I am using the Prototype library and ran into a serious limitation. I need to make many concurrent AJAX calls to the server, but Prototype is queueing them instead. Obviously,...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
5
by: steve.chambers | last post by:
I'm sure this q must have been asked before but I'm really struggling to find the answer anywhere so have finally given up and will consult the usenet community - hopefully there's someone out...
3
by: dhsieh | last post by:
I am trying out nested AJAX calls for the first time, but I seem to have hit a snag. The code snippet is the outer AJAX call and a function, it gathers information about a company. As we get towards...
7
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
我们现在遇到一个问题,通过wcf创建的webservice,选择windows service作为宿主,采用java作为客户端调用成功,但是无法使用asp.net ajax调用。...
1
by: kidalex | last post by:
So, I have a summary page of some stuff that gets updated through AJAX calls back to the server every 15 seconds or so. However, if you leave that page up for a few hours - it'll slow down the...
1
by: andwan0 | last post by:
I have a legacy classic ASP website with lots of classic AJAX (many ASP files specially made for processing AJAX requests). We are slowly migrating the website to ASP.NET 2.0 and developing under...
2
by: sdkumar00 | last post by:
How can we avoid/detect multiple free calls for a dynamically allocated memory. void example() { int *a; a = (int *)malloc(10); free(a); free(a); //how can we detect/avoid multiple...
4
by: Stephan Needank | last post by:
Hello, I'm encountering an AJAX problem when I try to execute multiple AJAX requests at the same time. What I want to do is delete a message and display the status (succes or failure) of that in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.