我正在试图制作一个从用户获取电子邮件标题的网页。当用户点击按钮来分析页面时会看到代码,并从电子邮件的标题中找到源IP地址。那么这个网页会用提取的ip地址数量向freegeoip.com查询。Ajax函数只运行一次
这里是问题所在。不管我从电子邮件头获得多少个IP地址,我的代码只运行一次ajax函数,然后再没有任何请求。
请问您可以帮我吗?
var myLatlngArray;
function iAnalyse()
{
//document.getElementById("result").innerHTML
//document.getElementById("headerValue").value;
var lines = document.getElementById("headerValue").value.split('\n');
var ipAddrs = [];
var cnt=0;
for(i=0; i<lines.length; i++)
{
\t if(lines[i].startsWith("Received: from"))
\t {
\t \t ipAddrs[cnt++]=ipAddr=lines[i].substring(lines[i].lastIndexOf("[")+1,lines[i].lastIndexOf("]"));
\t }
}
myLatlngArray=new Array(cnt);
for(j=0;j<cnt;j++)
\t getIPaddress(ipAddrs[j]);
//alert(ipAddrs.length);
}
//http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string
if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str){
return this.indexOf(str) == 0;
};
}
var xHRObject = false;
if (window.XMLHttpRequest)
{
xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function getIPaddress(ipAddrs)
{
\t xHRObject.open("GET", "http://freegeoip.net/json/"+ipAddrs+"?t=" + Math.random(),true);
\t xHRObject.onreadystatechange = getData;
\t xHRObject.send(null);
}
var aList=0;
function getData()
{
if ((xHRObject.readyState == 4) && (xHRObject.status == 200))
{
if (window.XMLHttpRequest)
{
alert("abc"); //codes here should be run as many as the number of cnt in iAnalyse function, but it is called only once..
var jsonObj = JSON.parse(xHRObject.responseText);
myLatlngArray[aList]=new Array(2);
\t myLatlngArray[aList][0]=jsonObj.latitude;
\t myLatlngArray[aList][1]=jsonObj.longitude;
aList++;
xHRObject = new XMLHttpRequest();
//google.maps.event.addDomListener(window, 'load', initialize());
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<script src="analyse.js"></script>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #googleMap {
width:1000px;
height:500px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
</head>
<body>
<p>IT Security <button onclick="iAnalyse()">Analyse it!</button> </p>
<textarea id="headerValue" rows="10" cols="140">Past your email header here.</textarea>
<p><div id="googleMap"></div></p>
</body>
</html>
您可以使用下面的代码测试电子邮件标题摆在textarea的。
`Delivered-To: [email protected]
Received: by 10.216.7.2 with SMTP id 2csp890396weo;
Sat, 13 Sep 2014 10:51:53 -0700 (PDT)
X-Received: by 10.236.230.70 with SMTP id i66mr19664921yhq.26.1410630711344;
Sat, 13 Sep 2014 10:51:51 -0700 (PDT)
Return-Path: <[email protected]>
Received: from iflip4 ([108.166.68.249])
by mx.google.com with ESMTPS id 24si1044060yhd.26.2014.09.13.10.51.49
for <multiple recipients>
(version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
Sat, 13 Sep 2014 10:51:51 -0700 (PDT)
Received-SPF: permerror (google.com: permanent error in processing during lookup of [email protected]) client-ip=108.166.68.249;
Authentication-Results: mx.google.com;
spf=permerror (google.com: permanent error in processing during lookup of [email protected]) [email protected];
dmarc=fail (p=NONE dis=NONE) header.from=ebay.com.au
Message-Id: <[email protected]m>
Received: from [194.1.180.85] (port=12990 helo=User)
by iflip4 with esmtpa (Exim 4.82)
(envelope-from <[email protected]>)
id 1XRT9o-0006MC-PP; Tue, 09 Sep 2014 21:41:02 +0000
From: "PayPal"<[email protected]>
`
是否'的getData()'每次被调用,就会出现相应的检查?否则,每次调用'getIPaddress' - 即是否抛出期望数量的'GET's?当你迭代ipAddrs []数组时,你可能希望通过'setTimeout'来交错你的http请求? – ne1410s 2014-09-26 14:55:27
您每次调用getIPaddress时都会取消先前的ajax请求,因为它没有时间处理,因此它取消了前一个请求并开始新的请求 – 2014-09-26 14:58:09
正如@PatrickEvans所说,您将取消每个“ getIPaddress'调用。您应该在每次调用时实例化一个新的'XHR',并使用闭包从回调函数提供对'XHR'对象的访问。 – 2014-09-26 15:19:55