admin管理员组文章数量:1334932
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
1 Answer
Reset to default 9Inside 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'
.
本文标签: javascriptSyntax error in jQueryparseJSONStack Overflow
版权声明:本文标题:javascript - Syntax error in jQuery.parseJSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742377389a2463433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论