Hey.. I'm trying to have more than 1 ajax request running on my site at a time, but it seems that as soon as a second request starts, the first one stops... I don't know if it's a limitation of javascript or its something I'm doing wrong, but if somebody could help me out that would be great... Thanks in advance LiNx88 <script> function AjaxConnection(url) { this.connect=connect; this.uri=url; } function connect(return_func) { with(this) { x=init_object(); x.open("POST", uri,true); x.onreadystatechange = function() { if (x.readyState != 4) return; eval(return_func + '(x.responseText)'); delete x; } x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); x.send(''); } } function init_object() { var x; try { x=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { x=new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) { x=null; } } if(!x && typeof XMLHttpRequest != "undefined") x = new XMLHttpRequest(); if (x) return x; } </script> <script> function clickme() { connection = new AjaxConnection("text.txt"); connection.connect("callBack"); } function clickme2() { connection2 = new AjaxConnection("big.txt"); connection2.connect("callBack"); } function callBack(content) { document.getElementById('hi').value = content; } </script> Code (markup):
I fixed it.. For future reference and if anybody's the least bit interested, the problem was with this function this.x.onreadystatechange = function() { if (self.x.readyState != 4) return; eval(return_func + '(self.x.responseText)'); delete self.x; } Code (markup): For some reason, the x's inside the new function were relating to the global scope instead of just the instance of the function... When I changed it to this function connect(return_func) { this.x=init_object(); this.x.open("GET", this.uri,true); var self = this; this.x.onreadystatechange = function() { if (self.x.readyState != 4) return; eval(return_func + '(self.x.responseText)'); delete self.x; } this.x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); this.x.send(''); } Code (markup): Everything worked perfectly. -LiNx88
LiNx88! you are absolutely brilliant!! i've been searching for so long to make multiple ajax request at the same time work but nobody gave a good solution except for you! i used your code and everything worked perfectly! thank you very much! vannazz
The A in Ajax stands for Asynchronous. So really, almost by definition you should be able to have multiple requests running simultaneously. To the original poster: why aren't you using a library? Whoa, I just realized how old the original post is.
@vannazz: If you are using PHP, please try my KoolAjax control Here is example of multiple requests: http://www.koolphp.net/support/demos/#koolajax.example_simultaneous_requests Or you may see this examples, 9 multiple requests at same time: http://www.koolphp.net/support/demos/#koolajax.example_loading_panel (Try to click other boxes when some boxes are loading) In the second example, you may find out that there is absolutely no javascript involved.
I registered here for the sole purpose of responding to your 5 year-old post to say simply, That was masterful. Thank you