javascript - 从 JavaScript angular js 中的 URL 成功获取数据(Ajax)

标签 javascript angularjs json ajax

我正在使用 Angular 移动 Ui 框架工作。我正在从 Controller 中的 URL 获取数据。但现在我想在成功部分获得特定数据。我将展示我的代码片段以获取提示。

HTML:

   <div class="jumbtron scrollable-content text-center bg-color">
   <div class="row">
          <div class="bgImage">  
          </div>
          <div class="btn-group img-responsive"  ng-controller="MyControllerCity"> 
              <div ng-repeat="prdct in cityProduct">
                   <a href="#/category-prduct" style="color:#763428; font-weight:bold;">
                      <img  src="{{prdct.categoryImage}}">
                    </a>
              </div>
          </div>

    </div>
</div>

HTML 输出
enter image description here

Controller :

.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; 
                console.log("aaaaaaa" + cityProduct.categoryId);

            }).error(function(data, status, headers ,config) {

        })

})

console.log("aaaaaaa" + cityProduct.categoryId);

但 categoryId 没有显示在控制台中。

JSON 屏幕截图: enter image description here

这是 JSON 响应:

[{
    "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);

但是出现了一些错误,我将向您展示控制台错误的屏幕截图: enter image description here

提前致谢。

最佳答案

您的 JSON 是一个对象数组,因此您需要像这样访问它:

$scope.cityProduct[0].categoryId

或者更动态的东西:

$scope.cityProduct.forEach(function(product) {
    console.log(product.categoryId);
});

另请注意,您在第一次设置 cityProduct 变量后错过了 $scope。

关于javascript - 从 JavaScript angular js 中的 URL 成功获取数据(Ajax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35220856/

相关文章:

javascript - 初始加载后,主干模型在项目 View 中未定义

javascript - 如何使用 AngularJS 对多个对象应用过滤器?

ios - 使用 JSON 的 iOS 谷歌搜索

php - Android:如何在客户端和服务器之间同步数据库条目

java - 将 Bing JSON 转换为 Java

javascript - 单击按钮时如何获取输入值

javascript - 使用 ng-autocomplete 时默认值未预填充输入字段

javascript - 我们可以在 html5 视频上每秒获得多少帧?

javascript - AngularJS $http.get 异步执行顺序

javascript - AngularJS Factory 在每次点击时加载数据