Hasif
Hasif

Reputation: 39

How to get client's country address Ip address using JavaScript?

How to get client Ip address , country details using javascript json

Upvotes: 0

Views: 3824

Answers (3)

melvin
melvin

Reputation: 2621

Use some webservice that can return json

jsonip.com

$(document).ready(function () {
    $.get('http://jsonip.com', function (res) {
        $('p').html('IP Address is: ' + res.ip);
    });
});

Smart-IP.net (leading one today)

$(document).ready(function () {
    $.getJSON('http://smart-ip.net/geoip-json?callback=?', function(data) {
        $('p').html('My IP Address is: ' + data.host);
    });
});

Upvotes: 0

user6299088
user6299088

Reputation:

Use ipapi.co api:

// https://ipapi.co/json/

 $.getJSON('https://ipapi.co/json/', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 6

Eugene Lisitsky
Eugene Lisitsky

Reputation: 12855

JavaScript doesn't provide such functionality. Usual solution is to make an endpoint on you server which returns IP address and query it in XHR request from a page.

Upvotes: 0

Related Questions