使用$ http.get时出现角度语法错误

时间:2016-11-19 17:21:06

标签: javascript angularjs json

我正在获取angular.min.js:117语法错误:在使用$ http.get()函数时,在位置8的JSON中出现意外的令牌我得到错误这是我的角度控制器

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);

        });

    });

这是我将json数据存储在classified.json文件中

[{
    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]

2 个答案:

答案 0 :(得分:0)

正如错误消息所示,问题在于您的JSON语法,而不是您的Angular语法。

JSON中的属性名称必须表示为字符串。它是JavaScript对象文字,您可以选择字符串或标识符。

id:"1",

以上引用显示您使用标识符。它需要是一个字符串。

"id":"1",

......你在整个过程中犯了类似的错误。

linting tool可能对您有用。

答案 1 :(得分:0)

Json应如下所示(从上次删除] [1]

[{
    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

    }


]