Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Taking out the A in AJAX
Old 11-21-2011, 05:22 PM Taking out the A in AJAX
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Hey guys,

I've been having some trouble in a small little script I'm making. What it does is generate IPs, then query those IPs and return the result. Basically, it returns all the webservers found on your LAN.

However, I've added a 'SCAN COMPLETED' string at the end of the scan. The problem is though, although the IPs are generated with a loop, so they're perfectly in order, AJAX doesn't return the results in order. So my results end up looking like this (limited to 5 IPs):


*click for a bigger image*

As you can see, the IPs queried are not in order; going from 0,1,2,3,5 (complete),4. The 4 shows up after the scan says it's complete, and even though it is, the display isn't correct.

The script works fine, but the output for the user is messed up. I've looked into the matter, and I've found that it's the Asynchronous-ness of AJAX that's returning the results weirdly. Whatever returns first is returned first, if that makes sense.

I've tried setting the async="false" attribute (new in HTML5), but that doesn't nothing for me. I'd really appreciate if somebody could quickly look at my code and tell me what's wrong with it. It's probably something stupid anyway.

HTML Code:
<?php

$ips='192.168.0.';
$limit=5;

if(isset($_GET['ip'])){
	$scan='http://'.$ips.$_GET['ip'];
	$s=@file_get_contents($scan);
	echo "trying <a href=\"$scan\">$scan</a>...";
	if($s!=null){echo " complete!";}
	else{echo " failed";}
	echo '<br/>';
	if($_GET['ip']==$limit){echo "SCAN COMPLETED";}
	exit;
}

?>
<script type="text/javascript">
function loadscan(scanip){
var xmlhttp;
if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function(){
  if(xmlhttp.readyState==4&&xmlhttp.status==200){
    document.getElementById("return").innerHTML=document.getElementById("return").innerHTML+xmlhttp.responseText;
  }
}
xmlhttp.open("GET","scanner.php?ip="+scanip,true);
xmlhttp.send();
}

function incel(){
	document.getElementById("el").innerHTML=parseInt(document.getElementById("el").innerHTML)+1;
}

function go(a){
	var complete=false;
	if(complete==false){
		setInterval("incel()",1000);
	}
	var i;
	for(i=0;i<=a;i++){
		loadscan(i);
	}
}

</script>
<button onclick="go(<?php echo $limit;?>);this.style.display='none';document.getElementById('ela2').style.display='block';">Scan</button><span id="ela2">Time Elapsed: <span id="el">0</span></span>
<div id="return"></div>
*Useless stuff removed*

You might notice that I am opening a new AJAX request each call. It's tedious, but it's one of the many attempts I've made to fix this small glitch that would otherwise make a perfectly working script.

Thanks!!

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 11-21-2011 at 05:24 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-21-2011, 05:33 PM Re: Taking out the A in AJAX
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Reuse the request object that should stop the buffering

http://ajaxian.com/archives/reusing-...-without-abort
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-22-2011, 09:48 PM Re: Taking out the A in AJAX
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Thanks, Chris! It works perfectly

Here's my current code, if anybody's interested:

Code:
//synchronous (SJAX) call!
function loadscan(scanip){
var xmlhttp;
if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
  if(xmlhttp){
     xmlhttp.open("GET","<?php echo basename($_SERVER['SCRIPT_NAME']);?>?ip="+scanip,false);                             
     xmlhttp.send(null);
     document.getElementById("return").innerHTML=xmlhttp.responseText+document.getElementById("return").innerHTML;
  }
  else{return false;}  
}
Of course, in the receiving PHP script, I have a 0.5 sec timeout (the response I'm querying is very, very fast) so nothing hangs forever.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 11-22-2011 at 09:50 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Taking out the A in AJAX
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12350 seconds with 12 queries