2

I am working in jQuery Mobile and PhoneGap for iPhone and Android!!

In my application I need to find out the IP address of the mobile device and POST to the server to get the details of residence and currency details from the server as its result (in JSON format).

I created some code; but it contains an external link, http://jsonip.appspot.com/?asp.net and its code looks like this:

<!DOCTYPE HTML>
<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>

But I am looking for a solution without using any server-side calling (http://jsonip.appspot.com/?asp.net) and server-side coding. And only using JavaScript which is OK on mobile devices. Is there a solution for that?

Or do I need to follow the given example which I checked?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ULLAS MOHAN.V
  • 1,521
  • 1
  • 19
  • 36
  • 1
    Your code worked perfectly? Whats the issue? – Satpal Jun 04 '13 at 10:44
  • http://simonmacdonald.blogspot.in/2012/08/so-you-wanna-write-phonegap-200-android.html – rags Jun 04 '13 at 10:47
  • possible duplicate of [How to get IP Address using phonegap javascript](http://stackoverflow.com/questions/12708330/how-to-get-ip-address-using-phonegap-javascript) – Gajotres Jun 04 '13 at 10:53
  • @Gajotres Please read my need before send its Duplicate Questions. i need without using external links.. http://stackoverflow.com/questions/12708330/how-to-get-ip-address-using-phonegap-javascript is I also checkd before when i post Q in this!! – ULLAS MOHAN.V Jun 04 '13 at 11:07
  • @Satpal I need to avoid the External links.. need to findout only using JavaScript and Client side ! – ULLAS MOHAN.V Jun 04 '13 at 11:13
  • 1
    @ULLASMOHAN.V You must either use a native plugin (the top-voted answer in the [proposed duplicate](http://stackoverflow.com/questions/12708330/how-to-get-ip-address-using-phonegap-javascript)) or server-side code. This cannot be done using only the current PhoneGap JavaScript APIs. – apsillers Jun 05 '13 at 13:34

4 Answers4

2

I needed the device IP address as well.

It turns out there is a PhoneGap 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.

I am posting this for people arriving via a search engine as it took me awhile to find the plugin.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user319862
  • 1,797
  • 2
  • 24
  • 32
  • from what I can tell this plugin just returns the ip address for the local wifi so something like 192... I need the public ip for security auditing, etc. – Nathan Prather Feb 10 '17 at 20:46
0
$(document).ready( function() { 
    $.getJSON( "http://smart-ip.net/geoip-json?callback=?", function(data){
        alert( data.host); 
    });
});
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • Did you real the part: `I created some code; but it contains an external link, http://jsonip.appsp...` and `...But I am looking for a solution without using any server-side calling` – Andreas Louv Feb 16 '15 at 11:24
0
    $.ajax({
        url: '//freegeoip.net/json/',
        type: 'POST',
        dataType: 'jsonp',
        success: function(location) {
            alert(location.ip);
        }
    });

This will support https sites too.

Ranjit Kumar
  • 273
  • 3
  • 16
0

Check out this post: how to edit local ip Address

Function (proimse) findIP is what you are looking for.

Community
  • 1
  • 1
jjyepez
  • 352
  • 3
  • 7