javascript - ReactJS returns parsererror SyntaxError: Unexpected token a after AJAX call to JAVA servlet -
i'm trying finish reactjs tutorial have error "parsererror syntaxerror: unexpected token a".
i have java servlet , post methods, send response next json:
"[{"author": "pete hunt", "text": "this 1 comment"}, {"author": "jordan walke", "text": "this comment"}]"
also tried 1 {"author": "pete hunt", "text": "this 1 comment"}, {"author": "jordan walke", "text": "this comment"}
i don't see problem. me looks missed in reactjs
upd code servlet response. i'm using gson library.
response.setcontenttype("application/json"); response.setcharacterencoding("utf-8"); modelobject obj = new modelobject(); obj.setauthor("pete hunt"); obj.settext("this 1 comment"); modelobject obj2 = new modelobject(); obj2.setauthor("jordan walke"); obj2.settext("this *another* comment"); list<modelobject> objlist = new arraylist<modelobject>(); objlist.add(obj); objlist.add(obj2); gson gson = new gson(); printwriter out = response.getwriter(); out.write(gson.tojson(objlist)); out.close(); out.flush()
when try parse string enclosed in redundant double quotes, this:
json.parse('"[{"author": "pete hunt", "text": "this 1 comment"}]"');
instead of:
json.parse(' [{"author": "pete hunt", "text": "this 1 comment"}] ');
then syntaxerror: unexpected token a
because tries parse "[{"
string , doesn't expect character a
next.
you have show exact command prints json string double quotes somewhere , unexpected token a
become unexpected token x
if change "author"
"x"
.
what doing (though speculation because haven't shown actual code) building json string manually never idea. should use library google gson or json-io you.
see also: java json serialization - best practice question.
Comments
Post a Comment