繁体   English   中英

Angular 1.6 $ http.get无法从json读取

[英]Angular 1.6 $http.get unable to read from json

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

最初,我尝试将json数据设置在控制器内部的变量上,并且一切正常,但是一旦移至JSON文件,便不会显示任何内容。 我的第一个错误是,我不知道JSON.parse()角度调用必须能够使用ANSI格式的JSON文件。 在将文件编码切换为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>

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

  1. 结束标记应为一个以关闭数组。 ]代替}
  2. JSON对象的最后一个字段不应包含逗号。 例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM