0

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 ?

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi tymeJV, Thank you for your suggestion. Now error has removed but on console its blank. Its not writing any thing on console. Can you help me to figure out this ?
Your http request is probably failing. Open your network tab and watch the status of the request
request call is happening becuase i m getting verification code on modile. but is this error failing response object ??? Error XMLHttpRequest cannot load api.nexmo.com/verify/…. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8080' is therefore not allowed access.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.