I am trying to use json to caputure visitor's IP address $.getJSON("http://jsonip.appspot.com?callback=?", function(data){ window.alert(data.ip); }); Code (markup): The above code displays the IP address in alert box. However I want to capture the result in a javascript variable $.getJSON("http://jsonip.appspot.com?callback=?", function(data){ var ip = data.ip; }); Code (markup): but that doesn't work. Really need some help on how to capture the json results in a custom javascript variable
It should work just fine. Perhaps you should change the scope of your variable. var ip; $.getJSON("http://jsonip.appspot.com?callback=?", function(data){ ip = data.ip; alert("My ip is " + ip); }); Code (markup):