GuitarGod Posted January 23, 2013 Share Posted January 23, 2013 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. Quote Link to comment Share on other sites More sharing options...
P5system Posted February 4, 2013 Share Posted February 4, 2013 You can write all the ajax function separately and call all of them in one function. It should work Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.