Jump to content

Firing multiple Ajax calls at the same time


GuitarGod

Recommended Posts

Hi guys,

 

In Ajax, I'm having trouble firing several Ajax calls at the same time. It seems luck of the draw which ones fire and which ones don't. Firing them one at a time or in a sequential order - they work fine, but trying to fire them all together makes some not work. Can anyone help?

Code examples

 

function ajax( action )
{
var ajaxRequest;

try
 {
// Opera 8.0+, Firefox, Safari

ajaxRequest = new XMLHttpRequest();
}

 catch ( e )
 {
// Internet Explorer Browsers

	 try
	 {
ajaxRequest = new ActiveXObject( 'Msxml2.XMLHTTP' );
}

	 catch ( e )
	 {
try
			 {
ajaxRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
}
			 catch ( e )
			 {
return FALSE;
}
}
}

// Ajax response!

 ajaxRequest.onreadystatechange = function()
 {
if( ajaxRequest.readyState == 4 )
	 {
				 if ( action == 'connection' )
				 {
						 // document.getElementById( 'element' ).innerHTML - ajaxRequest.responseText;
				 }
				 else if ( action == 'connection_methods_box' )
				 {
						 // document.getElementById( 'element' ).innerHTML - ajaxRequest.responseText;
				 }
				 else if ( action == 'connection_status' )
				 {
						 // document.getElementById( 'element' ).innerHTML - ajaxRequest.responseText;
				 }
}
}

// Send data to ajax.php

 if ( action == 'connection' )
 {
	 // Some paramaters here
 }
 else if ( action == 'connection_methods_box' )
 {
	 // More paramaters
 }
 else if ( action == 'connection_status' )
 {
	 // Even more
 }

// Send!

var data = 'action='+action;

ajaxRequest.open( 'POST', 'ajax.php', true );
ajaxRequest.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
 ajaxRequest.send( data );
}

 

In ajax.php, code is similar to:

 

<?php

if ( $_POST['action'] == 'connection' )
{
// stuff to do
}
etc..
etc..

?>

 

I know I could fire one Ajax query, then simply call the other once the first has finished, then the next one once the second has finished (and so on), but if possible I'd really like to fire them all at once.

 

Any help is appreciated.

Link to comment
Share on other sites

  • 2 weeks later...

When you get to the inner function, that's executed on the readyStateChange the action variable no longer exist. Which is why your code doesn't work.

You either need to send it along when creating the anonymous function, or trigger the action based upon the returned data from the server.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.