我不是一名专业的java脚本程序员。我希望你能帮助我。
我编写了一个使用ajax列出端口状态的代码。例如site:google.com端口:从24到80。但是当我尝试使用Javascript For循环列出输出时它不起作用。 代码是[index.php]:
<div id="inputform"><span>Enter URL or IP</span>
<input type="text" name="urladress" id="urladress" size=25 maxlength=100 value="<?php if (isset($_POST['urladress'])){echo $_POST['urladress'];}; ?>"></br>
from port
<input type="text" name="fromport" id="fromport" size=5 maxlength=6 value="<?php if (isset($_POST['fromport'])){echo $_POST['fromport'];}else{echo "1";} ; ?>">
to:
<input type="text" name="toport" id="toport" size=5 maxlength=6 value="<?php if (isset($_POST['toport'])){echo $_POST['toport'];}else{echo "24";} ;; ?>">
<input type="button" value="submit" onClick="getresults();" class="btn">
</div>
<div id="result" name="result"> </div>
<script language="javascript" type="text/javascript">
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Ajax not supported.");
return null;
}
}
// Change the value of the outputText field
function setOutput(){
document.getElementById('result').innerHTML += httpObject.responseText;
}
// Implement business logic
function getresults(){
document.getElementById('result').innerHTML = "calculating</br>"
httpObject = getHTTPObject();
if (httpObject != null)
{
var fromport = document.getElementById('fromport').value;
var toport = document.getElementById('toport').value;
if (fromport>toport || toport=='' || fromport==''){alert('wrong values');return 0;}
for(var i=fromport;i<=toport;i++){
httpObject.open("GET", "portscanner.php?inputText="+document.getElementById('urladress').value+"&port="+i, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
}
var httpObject = null;
</script>
</div>
服务器端[portscan.php]:
<?php
if (isset($_GET['inputText'])){
$url=$_GET['inputText'];
$url = str_replace("http://","",$url);
$url = str_replace("www.","",$url);
$port=$_GET['port'];
$fp = @fsockopen($url,$port,$errno,$errstr,2);
if(!$fp)
{
echo "port #". $port . "is closed</br>";
}
else
{
echo "Port #".$port."is open.</br>";
fclose($fp);
}
};
?>
它只显示上一个端口的结果: 例如site:http://stackoverflow.com ports:从70到80显示:
calculating
Port #80is open.
Port #80is open.
应该看起来像:
calculating
Port #70is closed.
Port #71is closed.
Port #72is closed.
...
Port #79is closed.
Port #80is open.
答案 0 :(得分:1)
每次循环执行时都会覆盖httpObject。您可能需要一个httpObjects数组来处理这个问题。
OnReadyStateChange不是阻塞命令,所以FOR循环将一直持续到最后一次。
更改这样的功能:
// Change the value of the outputText field
function setOutput(index){
if(this.readyState == 4) // Done
{
if(this.status == 200)
document.getElementById('result').innerHTML += this.responseText;
else
document.getElementById('result').innerHTML += "Error occured : " + this.status + "<br>";
}
}
// Implement business logic
function getresults(){
document.getElementById('result').innerHTML = "calculating</br>";
var fromport = document.getElementById('fromport').value;
var toport = document.getElementById('toport').value;
if (fromport>toport || toport=='' || fromport==''){alert('wrong values');return 0;}
for(var i=fromport;i<=toport;i++){
var index = httpObject.push(getHTTPObject()) - 1;
httpObject[index].open("GET", "test.php?inputText="+document.getElementById('urladress').value+"&port="+i, true);
httpObject[index].send(null);
httpObject[index].onreadystatechange = setOutput;
}
}
var httpObject = new Array();
答案 1 :(得分:0)
我写过一篇很长的答案,关于你如何只更新httpObject.responseText
的值,因为你决定是否在httpObject
存在的情况下调用服务器。
然后就在我即将点击“发布”时,我在函数末尾找到了var httpObject = null;
行。我也不知道JavaScript是否足以判断这是否是错误,但是尝试删除var
(我认为使变量本地化为封闭块 - 就像我说的那样,我是JavaScript的新手。)
更好的是,重写这些例程以避免httpObject
全局变量。也许传递一个带有httpObject.responseText
结果的字符串,而不是依赖于该对象存在于另一个函数中。
答案 2 :(得分:0)
我没有尝试过你的代码,但我认为你应该在循环中创建XmlHttpObject。用于多个异步请求的相同对象可能是个问题。