AngularJS从json获取数据

时间:2014-08-01 12:31:16

标签: json angularjs angularjs-scope

我正试图从Json数据中获取,但它对我不起作用。我不确定我做错了什么。

Angular Ajax API调用:

<script>
    function PostsCtrlAjax($scope, $http) {
        $http({
            method: 'POST',
            url: 'js/posts.json'
        }).success(function(data) {
            $scope.posts = data.deals;; // response data 
        });
    }
</script>

HTML数据绑定

<div id="ng-app" ng-app ng-controller="PostsCtrlAjax">
    <div ng-repeat="post in posts">
         <h2>
    <a href='{{post.url}}'>{{post.title}}</a>
    </h2>

        <div class='time'>{{post.time}} - {{post.author}}</div>
        <p>{{post.description}}</p>
        <img ng-src="{{post.banner}}" />
    </div>
</div>

Posts.json:

{
   "result":"SUCCESS",
   "resultMessage":"",
   "deals":[
      {
         "title":"Multiple Ajax Image Upload without Refreshing Page using Jquery.",
         "url":"http://www.9lessons.info/2013/08/multiple-ajax-image-upload-refreshing.html",
         "banner":"https://lh5.googleusercontent.com/-VWIAbbjR1QM/Uf_v9v9LCbI/AAAAAAAAIAo/4ZhYhP3lcCg/s550/multiple.jpg",
         "description":"Today I am presenting the most important social networking feature called multiple ajax image upload without refreshing the page using jquery and PHP. We just modified few lines of code in jqery.form.js plugin and renamed that to jquery.wallform.js. This feature is one of the key feature in Wall Script sale, big thanks to Arun Sekar for this code trick.",
         "time":"Tuesday, August 6, 2013",
         "author":"Srinivas Tamada"
      },
      {
         "title":"Wall Script 6.0",
         "url":"http://www.9lessons.info/2013/07/wall-script.html",
         "banner":"https://lh5.googleusercontent.com/-ErPa0tEQgLs/UfZzaQ3NcFI/AAAAAAAAH7o/h_KH8Rf4AXs/s550/WallBanner.jpg",
         "description":"Introducing the brand new Wall Script 6.0 with enrich social network features. Exactly year later I did released Wall Script previous version, it got awesome response and outstanding sale. This script IDEA was came out from my imagination, introducing powerful features like Conversations, OEmebd URL expanding system, Like/Share and Multiple image slider.",
         "time":"MONDAY, JULY 29, 2013",
         "author":"Srinivas Tamada"
      }
   ],
   "total":1207
}

我想从json文件标题,网址,横幅等获取。但我没有从我的json文件中获取任何数据。我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

您的JSON与教程不同,因此,您不应期望代码能够正常工作。你需要改变它。你试过这个吗?

<script>
  function PostsCtrlAjax($scope, $http) {
    $http({method: 'POST', url: 'js/posts.json'}).success(function(data) {
      $scope.posts = data.deals; // response data 
    });
  }
</script>

答案 1 :(得分:1)

您的JSON无效。对于buyDealUrl的每个成员,deals属性的值后面有一个尾随逗号。在最后一个deals对象之后,您还有一个尾随逗号。由于我们还没有看到你的代码,所以不可能知道这些是否是它不起作用的唯一原因,但这当然可能就是它。

删除这些逗号会为您提供有效的JSON:

{
    "result": "SUCCESS",
    "resultMessage": "",
    "deals": [
        {
            "id": "1001878",
            "title": "Cotton candy muffin danish applicake. Pie jujubes icing sesame snaps marshmallow tart. ",
            "description": "Halvah candy canes chupa chups toffee dessert jujubes wafer pie marshmallow.",
            "howToUse": null,
            "city": "",
            "provider": "Yelp",
            "realPriceWithSymbol": "530 EURO",
            "dealPriceWithSymbol": "199 EURO",
            "buyDealUrl": "http://www.example.com.com/satin-al/1001878/"
        },
        {
            "id": "100343",
            "title": "Cotton candy muffin danish applicake. Pie jujubes icing sesame snaps marshmallow tart. ",
            "description": "Halvah candy canes chupa chups toffee dessert jujubes wafer pie marshmallow.",
            "howToUse": null,
            "city": "",
            "provider": "Yelp",
            "realPriceWithSymbol": "530 EURO",
            "dealPriceWithSymbol": "199 EURO",
            "buyDealUrl": "http://www.example.com.com/satin-al/100343"
        }
    ],
    "total": 1207
}

答案 2 :(得分:1)

<script>
  function PostsCtrlAjax($scope, $http) {
    $http({method: 'POST', url: 'js/posts.json', dataType:"json", contentType:"application/json; charset=utf-8"}).then(function successCallback(data) {
      $scope.posts = data.data.deals; // response data 
    });
  }
</script>