【问题标题】:AngularJS "not well-formed", but still showing in firefoxAngularJS“格式不正确”,但仍在 Firefox 中显示
【发布时间】:2014-01-16 22:33:28
【问题描述】:

我刚刚开始使用 AngularJS 在网页上动态显示 json 数据。我以前使用 ng-repeat 和外部 json 文件就好了,但是当我在数组之前添加数据时,它不再在 IE 和 chrome 中工作,而是在 firefox 中显示一个 firebug 错误。我相信我的语法是正确的,但显然有问题,我想不通。

这是我的确切代码:

HTML

<html ng-app>
  <script src="angular.min.js"></script>
  <script src="dumctrl.js"></script>
  <head>
    <title>AngularJS Dummy Page</title>
  </head>
  <body ng-controller="AppCtrl">
    <table border="1px">
      <tr>
        <th>{{log.name}}</th>
        <th>{{log.xname}}</th>
        <th>{{log.yname}}</th>
      </tr>
      <tr>
        <th>ID</th>
        <th>X</th>
        <th>Y</th>
      </tr>
      <tr ng-repeat="entry in log.entries">
        <td>{{entry.id}}</td>
        <td>{{entry.x}}</td>
        <td>{{entry.y}}</td>
      </tr>
    </table>
  </body>
</html>

dumctrl.js

function AppCtrl($scope, $http) {
  $http.get('dummy.json').success(function(data) {
    $scope.log = data;
  });
}

dummy.json

{
  "name":"dummy",
  "xname":"xdummy",
  "yname":"ydummy",
  "entries":[
    {
      "id":0,
      "x":0,
      "y":0
    },
    {
      "id":1,
      "x":1,
      "y":0
    }
  ]
}

json 被验证了,它在 ff 中显示了预期的输出,我只是不知道出了什么问题。

【问题讨论】:

  • 你从 firebug 得到的语法错误是什么
  • 可能是同源安全问题 - 这将解释来自不同浏览器的不同行为。
  • Brad:刚要补充一点,萤火虫说在 dummy.json 第 1 行字符 1 上格式不正确 Billy Moon:我不确定这是问题所在,因为我几乎有一个副本,在 json 中只有一个数据数组,每次都可以正常工作。唯一的区别是在条目之前添加了名称字段。

标签: javascript json angularjs angularjs-ng-repeat


【解决方案1】:

我认为问题在于[{}] 之间的空格

我刚刚创建了一个JSFiddle,它运行良好:

var myApp = angular.module('myApp',[]);
function myCtrl($scope)
{    
    $scope.log = {
  "name":"dummy",
  "xname":"xdummy",
  "yname":"ydummy",
  "entries":[{
      "id":0,
      "x":0,
      "y":0
    },
    {
      "id":1,
      "x":1,
      "y":0
    }]
};

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2021-04-09
    • 2014-01-13
    相关资源
    最近更新 更多