0

i'm trying to retrieve the user's ip address from an external ip service. it works fine in the pc browser but it returns error 404 in the device. i've tried other external ip services but it ends up with the same error in the device. is there another way to get the device's external ip? how should i go about this?

here's my controller.js code:

$scope.findDeviceIP = function()
{
    $http.get("http://ipinfo.io/json")
    .success(function (data, status, headers, config) {
    alert('data is = ' +data);
    }).error(function (data, status, headers, config) {
    alert("error = " + data + "status = " + status);
    }); 
}

$scope.findDeviceIP();

and here's the error log:

error ip = {"data":"","status":404,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://ipinfo.io/json","headers":{"Accept":"application/json,text/plain,*/*"}},"statusText":"Not Found"}
Sydnie S
  • 303
  • 1
  • 5
  • 18

2 Answers2

1

Dont use .success use .then for your callback. You can get more information about it here

Try this

<html>
<head>
<script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>

<script>
 var app=angular.module("myapp", []);
 app.controller("namesctrl", function($scope,$http){
        $scope.findDeviceIP = function(){
         $http.get("http://ipinfo.io/json")
            .then(function (data, status, headers, config) {
                console.log('data is = ' +JSON.stringify(data));
            })
        }

        $scope.findDeviceIP();
 });

 
</script>


</head>
<body ng-app="myapp" ng-controller="namesctrl">

</body>
</html>
Community
  • 1
  • 1
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51
0

for it to work i placed the code inside a function called by a button.

Sydnie S
  • 303
  • 1
  • 5
  • 18