Angular在发布请求中发送用户IP

时间:2016-11-29 10:04:54

标签: angularjs

如何获取用户IP并在发布请求中发送?

.controller('LinkCtrl', function($scope, $http){

    $http.get("http://ipinfo.io").then(function(response){
        $scope.userIp = response.data
    })

    $scope.onClickLink = function($event){
        $http.post('http://localhost:7000/api/message', 
            {
                ip: userIp.ip
            })
    }    
})

我的尝试sais:ReferenceError:未定义userIp

1 个答案:

答案 0 :(得分:1)

您在变量名称之前忘记了$scope,您在scpe变量中添加了一个Ip并在没有范围的情况下使用它。所以错误。

.controller('LinkCtrl', function($scope, $http){

    $http.get("http://ipinfo.io").then(function(response){
        $scope.userIp = response.data
    })

    $scope.onClickLink = function($event){
        $http.post('http://localhost:7000/api/message', 
            {
                ip: $scope.userIp.ip
            })
    }    
})
相关问题