0

I'm learning AngularJS and Json and have a small page that needs to display data retrived from .json file in controller. When opening the page I have error: Unexpected token t at Object.parse (native)...

This is my html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../Scripts/angular.min.js"></script>
    <script src="../Scripts/Controllers/app.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="PostsCtrl">
        <ul ng-repeat="post in posts">
            <li>{{post.title}}</li>
        </ul>
    </div>
</body>
</html>

This is a controller:

var app = angular.module('myApp', []);
app.controller("PostsCtrl", function ($scope, $http) {
  $http.get('data/posts.json').
    success(function (data, status, headers, config) {
            $scope.posts = data;
    }).
    error(function (data, status, headers, config) {
    });
});

This is .json

[   
    {title: "Title1"},
    {title: "Title2"},
    {title: "Title3"}
]

I'm not sure why I get an error.

1 Answer 1

2

It seems like you are re parsing a json object with JSON.parse()?

or

json isnt valid

try

[   
    {"title": "Title1"},
    {"title": "Title2"},
    {"title": "Title3"}
]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This was the reason.
@gene glad it helped, voteup and accept as an asnwer so that it can help other also :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.