Arshpreet Arshpreet - 6 months ago 34
JSON Question

Get ip address from JSON/jquery

I have following Code for Get IP address i want to store that data.ip with global scope that i can access anywhere in file.

<script>
ip = null;
$.getJSON("http://jsonip.appspot.com?callback=?",
function(data){
ip = data.ip;
alert(ip); //return ip address correctly
});
alert(ip); //undefined or null
</script>

Answer

This will set the IP Address to a hidden input field.

<script>
ip = null;
$.getJSON("http://jsonip.appspot.com?callback=?",
  function(data){
       ip = data.ip;
 $("#getip").val(ip)
alert(ip); //return ip address correctly
  });
alert(ip); //undefined or null
<\script>

<input id="getip" type="hidden" />