Caliber Mengsk Posted September 30, 2011 Share Posted September 30, 2011 **Long post, so please bear with me. I'm completely new to ajax, I got my first ajax script running as of yesterday, so that's how fresh I am. I got it working for my most basic of basic needs. It opens a PHP page that connects to one of 40+ computers on a network based on ip, grabs the data from a MySQL database, and echos out the data to be received by Ajax. If I just use the one connection, it works fine. no problems. The thing is, I modified the php script that Ajax calls to look at all 40+ computers. That didn't work out so well as it kept timing out. This was pretty much expected being that each computer has several thousand results, resulting in tens of thousands (possibly hundreds of thousands) of columns of database data to look through collectively. So, what I decided to do, to make it easier on the system is to request the computer's information one at a time through ajax, rather then all at the same time through PHP, since ajax had no problems with searching one computer. Problem is, I now get "an error that says xmlhttp.status invalid_state_err dom exception 11". Also, through chrome's javascript debugging, I can look at the value of that xmlhttp.status and hover over it to see that it's value is 200, aka good file received. I can also find the ready state is indeed at 4 like it should be. I know it's not the PHP, as I can manually type in the requested URL that Ajax calls and it works fine (one computer at a time) but as soon as I try to call the ajax script more then once, it breaks like this. Any help? Ajax Script <head> <style> table{border:1px black solid;} tr{border:1px black solid;} td{border:1px black solid;} </style> <script type="text/javascript"> function loadXMLDoc() { var i; document.getElementById("Status").innerHTML = ""; for(i=2;i<=41;i++) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("Data").innerHTML = document.getElementById("Data").innerHTML + xmlhttp.responseText; //Don't know why, but the above line didn't like += as it stopped the next line from showing up. document.getElementById("Status").innerHTML += "System " + i + " finished.<br />"; }else{ document.getElementById("Status").innerHTML += "Failed to get data from system " + i + ". " + xmlhttp.status + "<br />"; } } document.getElementById("Status").innerHTML += "Getting data from system " + i + "<br />"; var url = "index.php?s=" + document.getElementById('serial').value + "&ip=" + i + "&random=" + Math.random(); xmlhttp.open("GET",url,true); xmlhttp.send(); } document.getElementById("Status").innerHTML += "Search complete through all systems."; } </script> </head> <html> <body> <input id='serial' /> <button type="button" onclick="loadXMLDoc()">Get Results</button> <div id="Status"><h2></h2></div> <div id="Data"><h2>Results will be here.</h2></div> </body> </html> I should note that if I get rid of the for loop and set i = to the ending digits of the ip address, it works fine. Like I said, individual ip's work, but I need to search through all the ip's between 2 and 41, and possibly more later down the road. I'd also like to note I tried setting it up as an array with xmlhttp[ i ] and it still returned the same results. Also, Chrome is saying that it gets the error 100+ times, which doesn't make sense since the for loop is only 40 long. Quote Link to comment https://forums.phpfreaks.com/topic/248198-new-to-ajax-connecting-to-multiple-sites-at-one-time-with-an-error/ Share on other sites More sharing options...
Caliber Mengsk Posted October 4, 2011 Author Share Posted October 4, 2011 Nevermind, I figured it out ^___^. Effectively just put a for look that calls the function independently rather then calling it linearly inside of the function. Freezes up the gif animations and such though. I thought of a way around that, but I don't feel like doing it XD. It's for internal use only anyway. function getData() { var i = 0; complete = 0; document.getElementById("Status").innerHTML = "Getting data. Please be patient.<br /><img src='images/loading.gif' />"; document.getElementById("Data").innerHTML = ""; var d = new Date(); for(i=rangeStart;i<=rangeEnd;i+=1) { loadXMLDoc(i); } } function loadXMLDoc(i) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("Data").innerHTML += xmlhttp.responseText; if(xmlhttp.responesText != "") { document.getElementById('system' + i).src = 'images/on.png'; } complete += 1; if(complete >= (rangeEnd-rangeStart)) { document.getElementById('Status').innerHTML = "<span class='good'>Completed getting information</span>"; } //Don't know why, but the above line didn't like +=. //document.getElementById("Status").innerHTML += "System " + i + " finished.<br />"; }else{ document.getElementById('system' + i).src = 'images/checking.gif'; //document.getElementById("Status").innerHTML += "Failed to get data from system " + i + ". " + xmlhttp.status + "<br />"; } } //document.getElementById("Status").innerHTML += "Getting data from system " + i + "<br />"; var url = "getData.php?s=" + document.getElementById('serial').value + "&ip=" + i + "&random=" + Math.random(); xmlhttp.open("GET",url,true); xmlhttp.send(); } Quote Link to comment https://forums.phpfreaks.com/topic/248198-new-to-ajax-connecting-to-multiple-sites-at-one-time-with-an-error/#findComment-1275756 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.