2

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.

3
  • data is already the parsed result. jquery.ajax: dataType "json": Evaluates the response as JSON and returns a JavaScript object. 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. Commented Nov 22, 2014 at 23:52
  • data isn't even a String. It's the parsed JSON object. Commented Nov 23, 2014 at 0:15

1 Answer 1

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'.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.