簡體   English   中英

AngularJS-控制器JavaScript $ http.get

[英]AngularJS - Controller JavaScript $http.get

我有以下代碼從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"
         ...

謝謝你的幫助!

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

這應該遍歷響應中的兩個對象,並記錄相關的單詞。

您缺少索引,請嘗試以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM