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.
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.
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']));
?>
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>
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.