Reputation: 317
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.
Upvotes: 2
Views: 3651
Reputation: 239541
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'
.
Upvotes: 9