0
var voted;

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
  voted = data.host;
  console.log(voted);
});

console.log(voted);

So voted is undefined outside, however, it's defined properly inside the function.

I'm wondering how I can use the data.value outside the function. The only thing I can think of is using a global variable, however, it doesn't work as expected.

Edit: Need the IP outside of functions

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
  voted(data.host);
});

function voted(ip) {
  ip_voted = ip_array.indexOf(ip);

  if (ip_voted > -1) {
    window.location.href = "results";
  }
}


if (choice && ip == '123123123') {

}
user1952811
  • 2,398
  • 6
  • 29
  • 49
  • 1
    You can't. Use a callback. How do you expect a value fetched by an asychrounous task to be available in the synchronous flow? – Johan Feb 20 '14 at 13:15
  • It'd be great if I could get a more comprehensive answer. I'm not the best at javascript. – user1952811 Feb 20 '14 at 13:16
  • You need to wrap you code that requires `data` in the callback, a.k.a. `function(data){ /* here */ }` – Johan Feb 20 '14 at 13:18

2 Answers2

0

you can use this way

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){

      writeVoted(data.host);
});

function writeVoted(voted) {
    console.log(voted);
}

or leave a synchronous call (I believe not be advisable)

async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default).      If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().
Paulo Lima
  • 1,238
  • 8
  • 8
0

The reason it doesn't work out for you is that the call to getJSON is an asynchronous call. In sense it means that it is ripped out of the current flow and executed somewhere independently. Take a look at the comments I have added to your code below:

var voted;

//getJSON is executed right away - async
$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
  //This callback function executes when the remote request responds. The timeframe is variable.
  voted = data.host;
  console.log(voted);
});

//Gets executed right away - doesn't wait for getJSON to get the remote response.
//voted is probably still undefined.
console.log(voted);

What you need to to is add a function that continues your flow, as shown below:

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
    voted(data.host);
});

function voted(ip) {
  ip_voted = ip_array.indexOf(ip);

  if (ip_voted > -1) {
    window.location.href = "results";
  }

   verify(ip);
}

function verify(ip) {
    if (choice && ip == '123123123') {

    }
}

If you really need to use the data in a global variable, you could "wait" for the response - however I strongly advise against doing this:

var host, value;
var responseArrived = false;

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
     host = data.host;
     value = data.value;
     responseArrived = true;
});

while( !responseArrived ) {
   //NOT A GOOD IDEA!
   //Will continue to loop until the callback of getJSON is called.
   //NOT A GOOD IDEA!
}

console.log(host);
console.log(value);
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • How do I get it into a variable outside of the functions? – user1952811 Feb 20 '14 at 13:27
  • Depends what you mean by this - under all circumstances you would need to wait for the response of the async call before you can use the value - even if stored in a global variable. – kaspermoerch Feb 20 '14 at 13:28
  • Well basically I need to use the IP elsewhere in my code (In an if condition) out side of the function. How would I do this without getting the IP into a variable. – user1952811 Feb 20 '14 at 13:32
  • When is this if-statement executed? Preferably you should delay the execution of it, until you have the data you need. – kaspermoerch Feb 20 '14 at 13:35
  • You can take a look at my updated question that provides the code. – user1952811 Feb 20 '14 at 13:36
  • You should move the if-statement into a function, that you call when the remote response has returned. I edited my answer to show what I mean. – kaspermoerch Feb 20 '14 at 13:39