AngularJS-控制器JavaScript$http.get
我有以下代码从JSON获取信息AngularJS-控制器JavaScript$http.get,javascript,arrays,angularjs,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) {
$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[i].result.all[j] where j=0....n
您的
响应
似乎是由2个对象组成的数组
替换:
console.log("words: ", response.result.all.Word);
与:
for(变量i=0;i
这应该迭代响应中的两个对象,并记录相关单词。U r not txtng。请不要这样写。响应是一个对象数组。根据JSON结果,对象位于第一个数组内。尝试添加索引响应[0]。结果。谢谢,但仍有错误“Word:未定义”。我还必须用“Words”进行迭代?这意味着
response[I].result.all
没有名为Word
的属性。尝试console.log(“单词:”,响应[i].result.all)“代码>看看它包含了什么。@卡洛斯,我想这是响应[i]。结果。所有[0]
@Cerbrus-ooops你是对的。”。非常感谢,它很有效@卡洛斯是我的得意门生
for(var i = 0; i < response.length; i++){
console.log("words: ", response[i].result.all.Word);
}