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.
datais already the parsed result. jquery.ajax: dataType"json": Evaluates the response as JSON and returns a JavaScript object.dataisn't even a String. It's the parsed JSON object.