AngularJS:当使用$http请求一个文件(json content)时发生SyntaxError

AngularJS: SyntaxError Occurs when requesting for a file (json content) using $http

本文关键字:一个 文件 json SyntaxError content AngularJS 请求 http      更新时间:2023-09-26

我需要使用$http服务访问一个文件。但是我得到以下错误在浏览器控制台

SyntaxError: Unexpected token '
    at Object.parse (native)
    at vc (http://localhost/libFile/angular.min.js:15:313)
    at $b (http://localhost/libFile/angular.min.js:81:301)
    at http://localhost/libFile/angular.min.js:82:216
    at n (http://localhost/libFile/angular.min.js:7:322)
    at ed (http://localhost/libFile/angular.min.js:82:198)
    at c (http://localhost/libFile/angular.min.js:83:382)
    at http://localhost/libFile/angular.min.js:119:302
    at m.$get.m.$eval (http://localhost/libFile/angular.min.js:134:83)
    at m.$get.m.$digest (http://localhost/libFile/angular.min.js:131:106)

下面是带有Angular表达式的html模板。这里没有错误

<html ng-app="nameApp">
<head>
  <meta charset="utf-8">
  <script src="http://localhost/libFile/angular.min.js">
  </script>
  <script>
  var nameApp = angular.module('nameApp',[]);
    nameApp.controller('myController' , function($scope,$http)
    {
      $http.get('http://localhost/checkangular/art_1.json').success(function(data){
          $scope.arts = data;
      });
    });
  </script>
</head>
<body ng-controller="myController">
<ul>
  <li ng-repeat="art in arts">
   {{art.movie}} - {{art.mainChar}}
  </li>
</ul>
</body>
</html>
我art_1

。json文件。

[
  {'movie' : 'i am legand','mainChar':'Will Smith'},
  {'movie' : 'Batman Begins','mainChar' : 'Christian Bale'},
  {'movie' : 'Man of Steel', 'mainChar' : 'Henry Cavill'}
];

我浏览了一下寻找解决方案,发现我可能错过了$http service中的参数或没有导入的模块。在在线教程中,就像他们做的一样,它对他们有效。

我正在使用AngularJS v1.4.0库
请在这里给我一个解决方案

这是因为它是无效的JSON。JSON使用"而不是',并且它不以;结束。

你可以使用JSONLint这样的工具来验证。您还可以阅读JSON及其语法。另外,不要手工构建JSON。这很容易产生很多错误。