我正在尝试使用jquery显示json数组的内容,但它返回此错误:
SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符
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); }
});
}
我已经使用高级rest客户端检查了返回的JSON字符串,这就是我得到的:
'[{"id":6,"nombre":"nombre1","apellidos":"apellido1","afiliacion":"aaa","nacionalidad":"bbb"}]'
看起来是正确的。
答案 0 :(得分:9)
在success
内,data
已经解析。您不需要使用JSON.parse
手动执行此操作。 jQuery为你做了那么多,这是使用dataType: 'json'
的全部目的。