我不是一个专业的java脚本程序员。我希望你能帮助我。
我写了一个代码来使用ajax列出端口状态。例如站点: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 show:
calculating
Port #80is open.
Port #80is open.
它应该看起来像:
calculating
Port #70is closed.
Port #71is closed.
Port #72is closed.
...
Port #79is closed.
Port #80is open.
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();
我写了一个很长的答案,关于您如何只更新httpObject.responseText
的值一次,因为您决定是否调用服务器,如果httpObject
存在与否。
然后就在我要点击"Post"的时候,我在函数的末尾发现了var httpObject = null;
行。我也不知道足够的JavaScript告诉如果这是错误,但尝试删除var
(我认为使变量局部封闭块-就像我说的,我在JavaScript很多新的)。
更好的是,重写这些例程以避免使用httpObject
全局变量。也许传递一个字符串与httpObject.responseText
的结果,而不是依赖于对象存在于另一个函数
我没有尝试过你的代码,但我认为你应该在循环中创建XmlHttpObject。用于多个异步请求的相同对象可能是一个问题。