Jump to content


Photo

Firing multiple Ajax calls at the same time


  • Please log in to reply
2 replies to this topic

#1 GuitarGod

GuitarGod

    Advanced Member

  • Members
  • PipPipPip
  • 87 posts

Posted 23 January 2013 - 05:41 AM

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.

#2 P5system

P5system

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationUSA

Posted 04 February 2013 - 05:02 AM

You can write all the ajax function separately and call all of them in one function. It should work

If you like my reply please click on https://plus.google....w.p5systems.com to approve it.


Thanks,
P5 Systems
http://www.p5systems.com/


#3 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 04 February 2013 - 04:37 PM

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.
Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com