从JavaScript js中的URL获取Suces(Ajax)中的数据
我使用的是angular mobile Ui框架。我正在从控制器中的URL获取数据。但现在我想在成功部分得到一些具体的数据。我将显示我的代码片段以获得提示 HTML: console.log(“aaaaaaa”+cityProduct.categoryId) 但类别未显示在控制台中 JSON屏幕截图: 以下是JSON响应:从JavaScript js中的URL获取Suces(Ajax)中的数据,javascript,angularjs,json,ajax,Javascript,Angularjs,Json,Ajax,我使用的是angular mobile Ui框架。我正在从控制器中的URL获取数据。但现在我想在成功部分得到一些具体的数据。我将显示我的代码片段以获得提示 HTML: console.log(“aaaaaaa”+cityProduct.categoryId) 但类别未显示在控制台中 JSON屏幕截图: 以下是JSON响应: [{ "categoryImage": "https://www.winni.in/assets/img/app/city-home/cake.jpg",
[{
"categoryImage": "https://www.winni.in/assets/img/app/city-home/cake.jpg",
"categoryName": "cakes",
"categoryId": 4
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/flowers.jpg",
"categoryName": "flowers-and-bouquets",
"categoryId": 12
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/chocolates.jpg",
"categoryName": "chocolates-for-all",
"categoryId": 63
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/mugs.jpg",
"categoryName": "Mugs",
"categoryId": 34
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/figurines.jpg",
"categoryName": "figurines",
"categoryId": 84
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/giftframes.jpg",
"categoryName": "gift-and-photo-frames",
"categoryId": 106
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/greetingcards.jpg",
"categoryName": "greeting-cards",
"categoryId": 103
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/handicrafts.jpg",
"categoryName": "handicrafts",
"categoryId": 44
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/penstands.jpg",
"categoryName": "pen-stands",
"categoryId": 142
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/combos.jpg",
"categoryName": "gift-combos",
"categoryId": 99
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/softtoys.jpg",
"categoryName": "soft-toys",
"categoryId": 104
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/diecasttoys.jpg",
"categoryName": "diecast-toys",
"categoryId": 138
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/jewellery.jpg",
"categoryName": "jewellery",
"categoryId": 41
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/clocks.jpg",
"categoryName": "clocks",
"categoryId": 137
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/crystalgifts.jpg",
"categoryName": "crystal gifts",
"categoryId": 136
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/lamps.jpg",
"categoryName": "lamps",
"categoryId": 97
}, {
"categoryImage": "https://www.winni.in/assets/img/app/city-home/slambook.jpg",
"categoryName": "slambook",
"categoryId": 139
}]
我想在控制台中获取categoryId
我用过:
console.log(“categoryId”+cityProduct[0].categoryId)
但是出现了一些错误,我将向您展示控制台错误的屏幕截图:
提前感谢。您的JSON是一个对象数组,因此您需要像这样访问它:
$scope.cityProduct[0].categoryId
或者更具活力的东西:
$scope.cityProduct.forEach(function(product) {
console.log(product.categoryId);
});
还请注意,在第一次设置cityProduct变量后,您遗漏了$scope。您的JSON是一个对象数组,因此您需要像这样访问它:
$scope.cityProduct[0].categoryId
或者更具活力的东西:
$scope.cityProduct.forEach(function(product) {
console.log(product.categoryId);
});
还请注意,在第一次设置cityProduct变量后,您漏掉了$scope。以下一行让您感到困惑:
$scope.cityProduct = data;
连续接收的数据是作为JSON响应的对象数组。使用索引访问数组元素,即$scope.cityProduct[0]
console.log("aaaaaaa" + cityProduct.categoryId);
在这行中,您应该在cityProduct.categoryId
之前编写$scope.
以下是固定代码:
.controller('MyControllerCity',function($scope, $http){
$http({
method:'get',
url:'http://192.168.0.3/sf/app/city-home/1',
headers: {'Content-Type': 'application/json'},
}).success(function(data, status,headers, config) {
$scope.cityProduct = data;
// to access first element's categoryId
console.log("aaaaaaa" + $scope.cityProduct[0].categoryId);
// iterate here to access all elements' categoryId
$scope.cityProduct.forEach(function(product) {
console.log(product.categoryId);
});
}).error(function(data, status, headers ,config) {
})
})
下面一行让你感到困惑:
$scope.cityProduct = data;
连续接收的数据是作为JSON响应的对象数组。使用索引访问数组元素,即$scope.cityProduct[0]
console.log("aaaaaaa" + cityProduct.categoryId);
在这行中,您应该在cityProduct.categoryId
之前编写$scope.
以下是固定代码:
.controller('MyControllerCity',function($scope, $http){
$http({
method:'get',
url:'http://192.168.0.3/sf/app/city-home/1',
headers: {'Content-Type': 'application/json'},
}).success(function(data, status,headers, config) {
$scope.cityProduct = data;
// to access first element's categoryId
console.log("aaaaaaa" + $scope.cityProduct[0].categoryId);
// iterate here to access all elements' categoryId
$scope.cityProduct.forEach(function(product) {
console.log(product.categoryId);
});
}).error(function(data, status, headers ,config) {
})
})
$scope.cityProduct!=cityProduct
是一个已弃用的.success
文件$scope.cityProduct!=cityProduct
是一个。success
是不受欢迎的产品,只是注意到我自己!早上很难:(修复了!@millerbr。我使用了console.log(cityProduct[0].categoryId)但是它显示了一些错误。@varunsharma我想我已经为你所犯的错误提供了答案——看看我的答案。如果你仍然面临问题,请告诉我。@millerbr。请检查我更新的问题。我只想显示当前的点击类别ID。如果点击蛋糕类别,那么蛋糕ID就会出现。如果点击鲜花类别,那么鲜花ID w我会来的。您已将数据保存在$scope.cityProduct中,但正在尝试在没有$scope部分的情况下访问-看看我的答案,您可以看到,在for循环行中,我包含了$scope.partYep,刚刚注意到我自己!早上很难:(修复了!@millerbr。我使用了console.log(cityProduct[0].categoryId)但是它显示了一些错误。@varunsharma我想我已经为你所犯的错误提供了答案——看看我的答案。如果你仍然面临问题,请告诉我。@millerbr。请检查我更新的问题。我只想显示当前的点击类别ID。如果点击蛋糕类别,那么蛋糕ID就会出现。如果点击鲜花类别,那么鲜花ID w我会来的。您已将数据保存在$scope.cityProduct中,但正在尝试在没有$scope部分的情况下访问-查看我的答案,您可以看到,在for循环行中,我包含了$scope.part。您的答案大致正确。但我一次只需要一个id。因为我希望根据类别显示数据。如person单击flowers然后只显示鲜花类别。我想将此类别Id附加到其他url上。我刚刚更新了我的问题。请检查。如果单击蛋糕类别,则蛋糕Id将出现。如果单击鲜花类别,则鲜花Id将出现。感谢您接受关于更正和投票的回答:)是的,您可以在html ng repeat中使用category_id并在url中使用来打开更改您的答案大致正确。但我一次只需要一个id。因为我想根据类别显示数据。像人一样单击花卉,然后只显示花卉类别。我想将此类别id附加到其他url上。只是我更新了我的问题。请检查。如果c点击蛋糕类别,然后蛋糕id就会出现。如果点击鲜花类别,那么鲜花id就会出现。感谢您接受答案并投票:)是的,您可以在html ng中使用类别id重复并在url中使用来打开更改