0

Hi i have a code like this

$(window).load(function(){

        var cookieString = document.cookie;
        var parsedCookie = cookieString.split(";");
        var hutk = '';
        for(var i=0; i<parsedCookie.length; i++){
            var eachCookie = parsedCookie[i].split('=');
            if(eachCookie[0] == ' hubspotutk'){
                hutk += eachCookie[1];
            }
        }

$.getJSON('http://jsonip.com/?callback=?', function(r){
   var ret = r.ip;
   return true;
 });

        var context = new Object();
        context.hutk = hutk;
        context.ipAddress = ret;
        context.pageUrl = window.location.href;
        context.pageName = document.title;
        context.redirectUrl = 'https://info.cos.net.au/thank-you';

        document.getElementById('contextJSON').value = JSON.stringify(context);

});

Ive tried to get and pass the IPAddress from the external server along with some other cookie values but it wont receive my ip address field. All other values are fine. The console.log does show IP address in the console.

THis bit here is causing the problem:

$.getJSON('http://jsonip.com/?callback=?', function(r){
   var ret = r.ip;
   return true;
 });

Can anyone suggest a solution? javascript accepted as well. thanks

Zen
  • 11
  • 1

1 Answers1

0

Ajax calls run asynchronously by default. This includes the $.getJSON, Try placing your code inside the $.getJSON call callback. Ex:

$.getJSON('http://jsonip.com/?callback=?', function(r){
   var ret = r.ip;
   return true;

   var context = new Object();
        context.hutk = hutk;
        context.ipAddress = ret;
        context.pageUrl = window.location.href;
        context.pageName = document.title;
        context.redirectUrl = 'https://info.cos.net.au/thank-you';

        document.getElementById('contextJSON').value = JSON.stringify(context);

 });
Pablo
  • 5,357
  • 6
  • 32
  • 48