外部ip服务返回404

时间:2017-04-25 09:25:11

标签: angularjs ionic-framework

我正在尝试从外部ip服务检索用户的IP地址。它在PC浏览器中工作正常但它在设备中返回错误404。我尝试过其他外部IP服务,但最终在设备中出现相同的错误。是否有其他方法来获取设备的外部IP?我该怎么办呢?

这是我的controller.js代码:

$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();

这是错误日志:

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"}

2 个答案:

答案 0 :(得分:1)

请勿使用.success使用.then进行回调。您可以获得有关它的更多信息here

试试这个



<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

让它工作我将代码放在一个按钮调用的函数中。