0

How to get Ip address in Jquery or Phonegap?

I am developing cross platform application for Android, Iphone and windows.

I need to find ip address of request.

user1912734
  • 23
  • 2
  • 5
  • 2
    You don't have access to the users IP adress on the clientside, see this SO question for solutions using a service and ajax : http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript – adeneo Mar 26 '13 at 12:43

3 Answers3

2

It's simple! ;)

 $.getJSON("http://jsonip.appspot.com",
    function(data){
       alert( "Your ip: " + data.ip);
  });

If you don't want to use http://jsonip.appspot.com and you have the possibility to run a php on a server, just replace http://jsonip.appspot.com with http://domain/ip.php, where ip.php is something like:

ip.php

<?php
   echo json_encode(array("ip"=>$_SERVER['REMOTE_ADDR']));
?>
JackTurky
  • 12,431
  • 41
  • 126
  • 208
0

try this

<html>
<head>
<script type="text/javascript" language="javascript">
  function myIP() {
    if (window.XMLHttpRequest) 
   xmlhttp = new XMLHttpRequest();
    else 
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.open("GET","http://jsonip.appspot.com/?asp.net",false);
    xmlhttp.send();

    hostipInfo = xmlhttp.responseText;
    obj = JSON.parse(hostipInfo);
    document.getElementById("IP").value=obj.ip;
    document.getElementById("ADDRESS").value=obj.address;
}
</script>
</head>
<body onload="myIP()">
IP :<input type="text" id="IP" name="IP" />
ADDRESS :<input type="text" id="ADDRESS" name="ADDRESS" />

</body>
</html>
Swapnil Tandel
  • 104
  • 1
  • 12
0

I needed the device ip as well. Turns out there is a phone gap plugin that returns the device's ip address from the OS.

See https://build.phonegap.com/plugins/679 and https://github.com/salbahra/NetworkInterfacePlugin/ for source code.

user319862
  • 1,488
  • 2
  • 21
  • 28