Angular 1.6 $ http.get无法从json

时间:2017-03-05 04:59:18

标签: javascript angularjs json http

我正在尝试从我在本地Web服务器上测试的静态json文件中读取json数据,但是使用$ http.get()服务无法显示任何内容。我在这里查看了一些其他类似的问题,但所有接受的答案都考虑了promise方法的使用,但是对于angularjs v1.6.x +这些已被折旧。

最初我尝试在控制器内部的变量上设置我的json数据,一切正常,但是一旦我移动到JSON文件,没有任何显示。我的第一个错误是我不知道JSON文件是ANSI格式,因为JSON.parse()角度调用能够工作。在将文件编码切换为ANSI之前,我遇到了语法错误,我的json结构是正确的。 (像Dreamweaver这样的文本编辑器将以UTF-8格式创建JSON文件。)

此时,当我检查网页时,控制台上没有任何JS错误,但根本没有数据出现。这是我有的: 我的events.json

     [
          {
            "day" : "Monday",
            "objective" : "Pipeline",
            "sessions" : [
            {
                "title" : "Leadership Excellence Luncheon",
                "start" : "11:30am",
                "end" : "1:00pm",
                "location": "room A"
            },
            {
                "title" : "Veteran Resume Workshop",
                "start" : "1:15pm",
                "end" : "2:00pm",
                "location": "room B",
                "speakers" : [
                {
                    "name": "John Doe",
                    "title": "Analyst",
                    "company": "Appel",
                    "headshot" : "http://placehold.it/119x134.jpg",
                    "bio": "john-doe/",
                },
                {
                    "name": "Jane Doe",
                    "title" : "VP",
                    "company": "Lancer",
                    "headshot" : "http://placehold.it/119x134.jpg",
                }
            ]
          }
        ]
    },
     {
        "day" : "Tuesday",
         "objective" : "Pipeline",
         "sessions" : [
           {
            "title" : "Leadership Excellence Luncheon",
            "start" : "11:30am",
            "end" : "1:00pm",
            "location": "room A"
        },
        {
            "title" : "Veteran Resume Workshop",
            "start" : "1:15pm",
            "end" : "2:00pm",
            "location": "room B",
            "speakers" : [
                {
                    "name": "John Doe",
                    "title": "Analyst",
                    "company": "Appel",
                    "headshot" : "http://placehold.it/119x134.jpg",
                    "bio": "john-doe/",
                },
                {
                    "name": "Jane Doe",
                    "title" : "VP",
                    "company": "Lancer",
                    "headshot" : "http://placehold.it/119x134.jpg",
                }
            ]
           }
          ]
        }
      }

这是我的app.js

(function() {
     var app = angular.module('Agendas',[]);


     app.controller('TableController', ['$scope', '$http',
    function($scope,$http) {
      $scope.title = "test";
      $scope.events = [];



     $http({
        method: 'POST',
        url: 'http://localhost/ang/data/events.json',       
    }).then( function(response) {
        $scope.events = response;
       });

    }]);

 })();

这是我的index.html

<!doctype html>
<html>
<head>
.....
</head>
<body>
....
<div id="agenda" class="mainCont container-fluid well" ng-app="Agendas" ng-controller="TableController">
 <div id="day" ng-repeat="event in events">
 <h1>{{ event.day }} &mdash; {{ event.objective }}</h1>
 <div id="sess" ng-repeat="session in event.sessions">
 <div style="width: 140px; float: left; clear: left;">{{ session.start }} - {{ session.end }}<br><br><em>Location: {{ session.location }}</em></div> <div style="float: left;"><em>{{ session.title }}</em>     <br>
    <div class="panelist" ng-repeat="speaker in session.speakers"><img ng-src="{{ speaker.headshot }}"><br>
    <a ng-href="{{ speaker.bio }}">{{ speaker.name }}</a><br>
    {{ speaker.title }} <br>
    {{ speaker.company }}</div>
   </div>
 </div>
<div class="aghr"></div>
 </div>     
<script type="text/javascript" src="js/angular.min.js"></script>
 <script type="text/javascript" src="js/app.js"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我注意到您的JSON文件包含一些错误。它可能是相关的。

  1. 结束标记应该是关闭数组的标记。 ]代替}
  2. JSON对象的最后一个字段不应该有逗号。 E.g:
  3. { "name": "John Doe", "title": "Analyst", "company": "Appel", "headshot" : "http://placehold.it/119x134.jpg", "bio": "john-doe/", <--- remove comma }

    您可以使用jsonlint网站来验证您的JSON。

    **根据建议,您可能必须先清除浏览器缓存。

    **另外更改

    $scope.events = response;
    

    $scope.events = response.data;