使用Angular.js从json文件获取数据时出错

时间:2015-12-31 15:00:37

标签: javascript angularjs json

从json文件中获取数据时出现以下错误。

  

错误:

SyntaxError: Unexpected token '
    at Object.parse (native)
    at uc (http://localhost/json/angularjs.js:15:480)
    at Zb (http://localhost/json/angularjs.js:82:229)
    at http://localhost/json/angularjs.js:83:143
    at m (http://localhost/json/angularjs.js:7:322)
    at cd (http://localhost/json/angularjs.js:83:125)
    at d (http://localhost/json/angularjs.js:84:380)
    at http://localhost/json/angularjs.js:118:334
    at n.$eval (http://localhost/json/angularjs.js:132:452)
    at n.$digest (http://localhost/json/angularjs.js:129:4

我正在解释下面的代码。

var test=angular.module('testapp', []);
test.controller('HelloController',function ($scope,$http) {
    console.log('hello');

    $http({
         method: 'GET',
         url: 'data.json',
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     }).then(function successCallback(response){

     },function errorCallback(response) {
        //$state.go('/',{}, { reload: true }); 
     });
  // Do something with myService
});
  

data.json:

[
{
    'id':1,
    'email':'abc@gmail.com',
    'name':'Rahul'
    },
    {
    'id':2,
    'email':'def@gmail.com',
    'name':'Ram'
    },
    {
    'id':3,
    'email':'ghi@gmail.com',
    'name':'praveen'
    }
]

请帮我解决此错误并获取所有数据。

1 个答案:

答案 0 :(得分:1)

Angular代码没问题。问题出在data.json文件中,使用"而不是'

data.json:

[
  {
    "id":1,
    "email":"abc@gmail.com",
    "name":"Rahul"
    },
    {
    "id":2,
    "email":"def@gmail.com",
    "name":"Ram"
    },
    {
    "id":3,
    "email":"ghi@gmail.com",
    "name":"praveen"
    }
]

您可以在enter image description here

上看到代码