简体   繁体   中英

Response object attribute coming undefined for angular rest call

I am trying to get attribute of reponse object but its coming "undefined"

var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope, $http) {


    $scope.addNewChoice = function () {
        console.log('aaaa');
        $http.get("http://api.nexmo.com/verify/json?api_key=569b1af&api_secret=d0de241&number=918650298011&brand=MyApp").success(function(response) {$scope.res = response.data;});
        console.log(res.request_id);
        console.log(res.status);
        console.log(res.error_text);

    };


});

Output on web console:

aaaa
angular.min.js:102 ReferenceError: res is not defined

Any idea what is incorrect in my code ?

All of your console.log statements are outside the $http callback (and are running before your request is complete - also, you have no variable named res - you have $scope.res ).

$http.get("http://api.nexmo.com/verify/json?api_key=56a9b1af&api_secret=d30de241&number=919650298011&brand=MyApp").success(function(response) {
    $scope.res = response.data;
    console.log($scope.res.request_id);
    console.log($scope.res.status);
    console.log($scope.res.error_text);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM