0

I am getting the following error while fetching data from my json file.

Error:

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

Here is my code:

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':'[email protected]',
    'name':'Rahul'
    },
    {
    'id':2,
    'email':'[email protected]',
    'name':'Ram'
    },
    {
    'id':3,
    'email':'[email protected]',
    'name':'praveen'
    }
]

How can I get all the data?

4
  • 3
    Try using double-quotes, ", in your JSON instead of single-quotes, '. Commented Dec 31, 2015 at 15:05
  • i did as per you..still there is error. Commented Dec 31, 2015 at 15:06
  • Is it the same error? As @whistling_marmot noted the first error seems to be because of single quotes. Try removing the headers object from the $http call, as you're not POST-ing anything. Commented Dec 31, 2015 at 15:10
  • yes error is same.its one path error i think.When i did /data.json this error is not coming but can not get the proper file.When i did again projectfoldername/data.json again wrong path is also coming. Commented Dec 31, 2015 at 15:13

1 Answer 1

1

The Angular code is ok. The problem is in data.json file, use " not '.

data.json:

[
  {
    "id":1,
    "email":"[email protected]",
    "name":"Rahul"
    },
    {
    "id":2,
    "email":"[email protected]",
    "name":"Ram"
    },
    {
    "id":3,
    "email":"[email protected]",
    "name":"praveen"
    }
]

You can see the code running on http://plnkr.co/edit/LjrqDMolGoaQn96ds6fv?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

@whistling_marmot already said this in his comment to the original message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.