AngularJS - Controller JavaScript $ http.get

时间:2014-10-21 08:32:29

标签: javascript arrays angularjs

我有以下代码从JSON获取信息。

$http.get('http://localhost:3000/folder/'Id)
     .success(function (response) {
           console.log("response ", response);
           console.log("words: ", response.result.all.Word);
     })
     .error(function (response) {
           console.log("error");
     });

但我在数组中获取信息时遇到问题:

TypeError: Cannot read property 'all' of undefined

回复中,我有:

response  [Object, Object]
  0: Object
    _id: "543e95d78drjfn38ed53ec"
     result: Object
       all: ObjectWord: Array[17]
        0: "word1"
        1: "word2"
        2: "word3"
         ...

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您的response似乎是一个包含2个对象的数组。

替换:

console.log("words: ", response.result.all.Word);

使用:

for(var i = 0; i < response.length; i++){
    console.log("words: ", response[i].result.all.Word);
}

这应该迭代响应中的两个对象,并记录相关的单词。

答案 1 :(得分:0)

您缺少索引,请尝试以下代码:

response[i].result.all[j]   where j=0....n