Skip to content Skip to sidebar Skip to footer

Get Client Ip In Javascript/angularjs

I'm trying to get client IP in my angular app. I'm using this code: $http.get('http://l2.io/ip.js').then(function(response) { $scope.ip = response.data; }); But it m

Solution 1:

There is a service provider that provides CORS headers (Access-Control-Allow-Origin: *) for javascript based requests:

https://freegeoip.com

Test the following XMLHTTP request for a sample:

var xhttp = newXMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log(xhttp.responseText)
    }
};
xhttp.open("GET", "//freegeoip.net/json/?callback=", true);
xhttp.send();

Or in case of jQuery Use:

$.getJSON('//freegeoip.net/json/?callback=?', function(data) {
  console.log(data);
});

Post a Comment for "Get Client Ip In Javascript/angularjs"