javascript 'SyntaxError: Unexpected token o' 与 angularjs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21307830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:37:37  来源:igfitidea点击:

'SyntaxError: Unexpected token o' with angularjs

javascriptjsonangularjs

提问by pyprism

It gaves me error when I m trying to call this controller .

当我尝试调用这个控制器时,它给了我错误。

hiren.controller('hirenz' , function($scope , $http , $location , $routeParams){
    $http.post((rootURL + "music") , {'alpha' : $routeParams.alpha , 'name' : $routeParams.name ,
        'album' : $routeParams.albumname }).success(function(data){
        var parsedJson = JSON.parse(data) ;
        console.log(parsedJson.name);
    });
});

Here is the "data" that i am calling from server

这是我从服务器调用的“数据”

{
    "name": [
        "Adhar - Adhar ",
        "Adhar - Adhare Opshori ",
        "Adhar - Aj Neshay "
    ],
    "url": [
        "http://music-com-bd.com/Music/A/Adhar/Adhare Opshori/Adhar - Adhar (music.com.bd).mp3",
        "http://music-com-bd.com/Music/A/Adhar/Adhare Opshori/Adhar - Adhar (music.com.bd).mp3",
        "http://music-com-bd.com/Music/A/Adhar/Adhare Opshori/Adhar - Adhar (music.com.bd).mp3"
    ]
}

回答by Jayram

You are parsing something which is not a string. It might be already in form of JSON object. You do not need to parse it. If you change var parsedJson = JSON.parse(data) ;to var parsedJson = data;The error will go off.

您正在解析不是字符串的内容。它可能已经是 JSON 对象的形式。您不需要解析它。如果您更改var parsedJson = JSON.parse(data) ;var parsedJson = data;错误将消失。

回答by Alex Postnikov

This error occurs when JSON is not properly formed. Check it with JSLint

当 JSON 格式不正确时会发生此错误。用JSLint检查它