0

I am trying to return a JSON string upon a AJAX callback.

I have kept my code as simple as I can, however I find that the callback is always error, and the error message is:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I use firebug to check the JSON response, and there is a strange character at the start of the response string (the one with red square in the attached picture)

enter image description here

And here is my Servlet:

response.setContentType("application/json;charset=UTF-8");

final JsonObject jsonResponse = new JsonObject();
.
.
.
.
jsonResponse.addProperty("result", "success");
final String jsonResponseString = jsonResponse.to![enter image description here][2]String();
response.getWriter().write(jsonResponseString);

Could anyone give me some help?

I feel like I am one step behind the answer, but I just cannot get it.

Thanks~

Hei
  • 513
  • 3
  • 13
  • 29

1 Answers1

0

for me it looks like an issue with the serialization of the response. Better approach is to fix that error from your back end rather than struggling with JSON string.

found a similar answer here in stackoverflow. hope it will help you to sort it out. LINK

Community
  • 1
  • 1
Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43