2014-09-26 115 views
3

我正在试图制作一个从用户获取电子邮件标题的网页。当用户点击按钮来分析页面时会看到代码,并从电子邮件的标题中找到源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 &nbsp;&nbsp;&nbsp;<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]> 
` 
+0

是否'的getData()'每次被调用,就会出现相应的检查?否则,每次调用'getIPaddress' - 即是否抛出期望数量的'GET's?当你迭代ipAddrs []数组时,你可能希望通过'setTimeout'来交错你的http请求? – ne1410s 2014-09-26 14:55:27

+2

您每次调用getIPaddress时都会取消先前的ajax请求,因为它没有时间处理,因此它取消了前一个请求并开始新的请求 – 2014-09-26 14:58:09

+0

正如@PatrickEvans所说,您将取消每个“ getIPaddress'调用。您应该在每次调用时实例化一个新的'XHR',并使用闭包从回调函数提供对'XHR'对象的访问。 – 2014-09-26 15:19:55

回答

1

如果你有一个以上的AJAX任务你网站,您应该创建 ONE标准函数来创建XMLHttpRequest对象,并且为每个AJAX任务调用 。

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

尝试是这样的:

function createXhr(){ 
    var xHRObject = false; 
    if (window.XMLHttpRequest) { 
    xHRObject = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
    xHRObject = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    return xHrObject; 
} 

,然后您可以:

function getIPaddress(ipAddrs) { 
    var xHRObject = createXhr(); 
    ... 
} 

和:

function getData(event){ 
    var xHrObject = event.target; 
    ... 
} 

如果你担心CORS,你可能想尝试这样的事:

function createCORSRequest(method, url) { 
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr) { 
    // XHR for Chrome/Firefox/Opera/Safari. 
    xhr.open(method, url, true); 
    } else if (typeof XDomainRequest != "undefined") { 
    // XDomainRequest for IE 9 and earlier. 
    xhr = new XDomainRequest(); 
    xhr.open(method, url); 
    } else { 
    // CORS not supported. 
    xhr = null; 
    } 
    return xhr; 
} 

来源:http://docs.webplatform.org/wiki/apis/xhr/XMLHttpRequest

+0

是啊,看起来更像是如何使任何微不足道的事情在IE中工作:)不错的一个 – Winchestro 2014-09-26 16:04:50

1

你的代码有一些问题,实际上并不是一个闭包。但是您实际上只有一个xhr请求用于您的所有请求,但这并不成功。您需要为每个请求创建XMLHttpRequest的new实例。这应该可以解决它:)

通过在请求函数的范围内移动响应处理函数,您可以通过生成的闭包访问原始的xhr请求。

(顺便说一句,我不知道我是否正确归了XMLHttpRequest接口微软的“东西”,它基本上只是一个猜测。)

var myLatlngArray; 

function iAnalyse(){ 
    var lines = document.getElementById("headerValue").value.split('\n'); 
    var ipAddrs = []; 
    var cnt=0; 

    for(i=0; i<lines.length; i++){ 
    if(lines[i].startsWith("Received: from")){ 
     ipAddrs[cnt++]=ipAddr=lines[i].substring(lines[i].lastIndexOf("[")+1,lines[i].lastIndexOf("]")); 
    } 
    } 
    myLatlngArray=new Array(cnt); 
    for(j=0;j<cnt;j++){ 
    getIPaddress(ipAddrs[j]); 
    } 
} 
if (typeof String.prototype.startsWith != 'function') { 
    String.prototype.startsWith = function (str){ 
    return this.indexOf(str) == 0; 
    }; 
} 
window.XMLHttpRequest=XMLHttpRequest||function(){ 
    return ActiveXObject&&ActiveXObject("Microsoft.XMLHTTP") 
}; 


function getIPaddress(ipAddrs){ 
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", "http://freegeoip.net/json/"+ipAddrs+"?t=" + Math.random(),true); 
    xhr.onreadystatechange = getData; 
    xhr.send(null); 

    function getData(){ 
    if(xhr.readyState == xhr.DONE && xhr.status == 200){ 
     var jsonObj = JSON.parse(xhr.responseText); 
     myLatlngArray[aList]=new Array(2); 
     myLatlngArray[aList][0]=jsonObj.latitude; 
     myLatlngArray[aList][1]=jsonObj.longitude; 
     aList++; 
    } 
    } 
} 

var aList=0;