Ajax function only works once

I am trying to create a web page that receives the email header from the user. when the user clicks the button to parse the page, it sees the code and discovers the original IP addresses from the email header. then this web page asks freegeoip.com with the number of extracted ip addresses.

here is the problem. no matter how many IPs I got from the email header, my code only fires the ajax function once and then there are no more requests.

Could you help me with this?

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++)
  {
  	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]);

  //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)
{
	xHRObject.open("GET", "http://freegeoip.net/json/"+ipAddrs+"?t=" + Math.random(),true);
	xHRObject.onreadystatechange = getData;
  	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);
	    myLatlngArray[aList][0]=jsonObj.latitude;
	    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>
      

Run codeHide result


You can use the following code for the title of the test message to be placed in the text box.

`Delivered-To: xxxxxxx@gmail.com
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: <eBay@ebay.com.au>
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 eBay@ebay.com.au) client-ip=108.166.68.249;
Authentication-Results: mx.google.com;
       spf=permerror (google.com: permanent error in processing during lookup of eBay@ebay.com.au) smtp.mail=eBay@ebay.com.au;
       dmarc=fail (p=NONE dis=NONE) header.from=ebay.com.au
Message-Id: <54148437.a401ec0a.35e9.ffffe2e6SMTPIN_ADDED_MISSING@mx.google.com>
Received: from [194.1.180.85] (port=12990 helo=User)
  by iflip4 with esmtpa (Exim 4.82)
  (envelope-from <eBay@eBay.com.au>)
  id 1XRT9o-0006MC-PP; Tue, 09 Sep 2014 21:41:02 +0000
From: "PayPal"<eBay@eBay.com.au>
`

      

+3


source to share


2 answers


If you have multiple AJAX tasks on your site, you must create ONE standard function to create an XMLHttpRequest object and call this for each AJAX task.

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

Try something like this:

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

      

and then you can:

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

      



and

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

      

If you are worried about CORS, you can try something like this:

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;
}

      

source: http://docs.webplatform.org/wiki/apis/xhr/XMLHttpRequest

+1


source


There were several issues in your code, the closure was not actually there. But you did have one xhr request that you used for all of your requests and it won't work. You need to create an new

XMLHttpRequest instance for every request. This should solve it :)

By moving the response handler function to the scope of your request function, you can access the original xhr request via the generated closure.



(btw I'm not sure if I normalized the XMLHttpRequest interface correctly for Microsoft's "thing", this is mostly just a guess.)

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;

      

+1


source







All Articles