角度对象调用的响应对象属性未定义 [英] Response object attribute coming undefined for angular rest call

查看:32
本文介绍了角度对象调用的响应对象属性未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取响应对象的属性,但是它即将未定义"

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

    };


});

Web控制台上的输出:

Output on web console:

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

您知道我的代码中有什么不正确吗?

Any idea what is incorrect in my code ?

推荐答案

您的所有 console.log 语句都在 $ http 回调之外(并且在您的请求已完成-同样,您没有名为 res 的变量-您具有 $ scope.res ).

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);
});

这篇关于角度对象调用的响应对象属性未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆