最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Syntax error in jQuery.parseJSON - Stack Overflow

programmeradmin1浏览0评论

I'm trying to show the content of a json array with jquery but it returns this error:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

function getMembers(){
 $.ajax({
    type: "GET",
    dataType: "json",
    url: "http://localhost:8080/rest/congreso/miembro/members",
    success: function(data){
        //if i use the next line works correctly
        //var json = jQuery.parseJSON( '[{"id":6,"nombre":"nombre1","apellidos":"apellido1","afiliacion":"aaa","nacionalidad":"bbb"}]' )
        //if i use the next line  i have a syntax error
        var json = jQuery.parseJSON(data);
        $.each(json, function(idx, obj) {
        $( "#resMembers" ).append( "<p>"+obj.nombre)+"</p>";
        });
        },
    error:function(res){
        alert("ERROR: "+ res.statusText); }
 });
}

I've checked the returned JSON string with advanced rest client and this is what I get:

'[{"id":6,"nombre":"nombre1","apellidos":"apellido1","afiliacion":"aaa","nacionalidad":"bbb"}]'

It looks correct.

I'm trying to show the content of a json array with jquery but it returns this error:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

function getMembers(){
 $.ajax({
    type: "GET",
    dataType: "json",
    url: "http://localhost:8080/rest/congreso/miembro/members",
    success: function(data){
        //if i use the next line works correctly
        //var json = jQuery.parseJSON( '[{"id":6,"nombre":"nombre1","apellidos":"apellido1","afiliacion":"aaa","nacionalidad":"bbb"}]' )
        //if i use the next line  i have a syntax error
        var json = jQuery.parseJSON(data);
        $.each(json, function(idx, obj) {
        $( "#resMembers" ).append( "<p>"+obj.nombre)+"</p>";
        });
        },
    error:function(res){
        alert("ERROR: "+ res.statusText); }
 });
}

I've checked the returned JSON string with advanced rest client and this is what I get:

'[{"id":6,"nombre":"nombre1","apellidos":"apellido1","afiliacion":"aaa","nacionalidad":"bbb"}]'

It looks correct.

Share Improve this question edited Nov 22, 2014 at 23:54 Bill the Lizard 406k212 gold badges574 silver badges891 bronze badges asked Nov 22, 2014 at 23:48 AFSAFS 3171 gold badge4 silver badges7 bronze badges 3
  • data is already the parsed result. jquery.ajax: dataType "json": Evaluates the response as JSON and returns a JavaScript object. – t.niese Commented Nov 22, 2014 at 23:52
  • Given that the JSON you posted does indeed look perfectly valid but also that you are getting an error, the first thing I'd suggest is writing the ACTUAL data passed to parseJSON to the console to validate that what you THINK is being passed to the JSON parser is correct. – Kolban Commented Nov 22, 2014 at 23:52
  • data isn't even a String. It's the parsed JSON object. – Derek 朕會功夫 Commented Nov 23, 2014 at 0:15
Add a ment  | 

1 Answer 1

Reset to default 9

Inside success, data is already parsed. You don't need to do that manually with JSON.parse. jQuery does that much for you, this is the entire purpose of using dataType: 'json'.

发布评论

评论列表(0)

  1. 暂无评论