我正在試圖製作一個從用戶獲取電子郵件標題的網頁。當用戶點擊按鈕來分析頁面時會看到代碼,並從電子郵件的標題中找到源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