tayyab vohra tayyab vohra - 15 days ago 10
AngularJS Question

Angular syntax error while using $http.get

I am getting angular.min.js:117 SyntaxError: Unexpected token i in JSON at position 8 while using $http.get() function here I am getting the error this is my angular Controller

angular syntax error

app.controller("classifiedCtrl", function($scope, $http){
$http.get('http://localhost/classified/data/classified.json').then(function(data){
$scope.classifieds=data;
console.log(data);

});

});


this is my json data which i have stored it in classified.json file

[{
id:"1",
title:"Pehla Item",
description:"This is the first description of the product and I am testing the product!",
price:500,
posted:"10-12-2015",
contact: {
name: "Tayyab Vohra",
phone:"055-055-055",
email:"johndoe@gmail.com"
},
categories:[
"Vehicles",
"Parts and Accessories"
],
images:"images/images.jpg",
views:134

},
{
id:"2",
title:"Dosra Item",
description:"This is the first description of the product and I am testing the product!",
price:200,
posted:"4-21-2015",
contact: {
name: "Tayyab Vohra",
phone:"055-055-055",
email:"johndoe@gmail.com"
},
categories:[
"Vehicles",
"Parts and Accessories"
],
images:"images/images.jpg",
views:233

},
{
id:"3",
title:"Third Item",
description:"This is the first description of the product and I am testing the product!",
price:500,
posted:"03-06-2015",
contact: {
name: "Tayyab Vohra",
phone:"055-055-055",
email:"johndoe@gmail.com"
},
categories:[
"Vehicles",
"Parts and Accessories"
],
images:"images/images.jpg",
views:233

}


]][1]

Answer

As the error message says, the problem is with your JSON syntax, not your Angular syntax.

Property names in JSON must be represented as strings. It is JavaScript object literals where you have the choice of strings or identifiers.

id:"1",

The above quote shows you using an identifier. It needs to be a string.

"id":"1",

… you make similar mistakes throughout.

A linting tool would probably be useful to you.

Comments