Having some trouble reading a json file in AngularJS using $http.get and encountering errors in the browser console.
Check out my Plunker example: http://plnkr.co/edit/SDRpu1MmrTPpgpdb6Kyk?p=preview
Below is the code snippet:
Javascript:-
var jayApp = angular.module('jayApp', []);
jayApp.controller('jayController', function($scope, $http){
$scope.entities = {};
$http.get("test.json").success(
function(data, status, headers, config) {
$scope.entities = data;
}
);
})
HTML
<!DOCTYPE html>
<html>
<head>
<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1838e8e959295938091a1d2cfd2cfd4">[email protected]</a>" data-semver="3.3.5" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c5c8c8d3d4d3d5c6d7e79489948992">[email protected]</a>" data-semver="3.3.5" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.5/cosmo/bootstrap.min.css" />
<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d3dedec5c2c5c3d0c1f1829f829f84">[email protected]</a>" data-semver="3.3.5" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e48e959181969da4d5cad5d5cad7">[email protected]</a>" data-semver="1.11.3" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d1dcdcc7c0c7c1d2c3f3809d809d86">[email protected]</a>" data-semver="3.3.5" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb9a959c8e979a89d59188bbcad5cfd5cc">[email protected]</a>" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app='jayApp' ng-controller='jayController'>
<h1>Read Json from file </h1>
Data : {{entities}}
</body>
</html>
Update:
Error
SyntaxError: Unexpected token '
at Object.parse (native)
at fromJson (https://code.angularjs.org/1.4.7/angular.js:1252:14)
at defaultHttpResponseTransform (https://code.angularjs.org/1.4.7/angular.js:9414:16)
at https://code.angularjs.org/1.4.7/angular.js:9505:12
at forEach (https://code.angularjs.org/1.4.7/angular.js:336:20)
at transformData (https://code.angularjs.org/1.4.7/angular.js:9504:3)
at transformResponse (https://code.angularjs.org/1.4.7/angular.js:10276:23)
at processQueue (https://code.angularjs.org/1.4.7/angular.js:14745:28)
at https://code.angularjs.org/1.4.7/angular.js:14761:27
at Scope.$eval (https://code.angularjs.org/1.4.7/angular.js:15989:28)(anonymous function) @ angular.js:12477(anonymous function) @ angular.js:9246processQueue @ angular.js:14753(anonymous function) @ angular.js:14761Scope.$eval @ angular.js:15989Scope.$digest @ angular.js:15800Scope.$apply @ angular.js:16097done @ angular.js:10546completeRequest @ angular.js:10744requestLoaded @ angular.js:10685