0

I currently have a servlet returning a JSON string on a POST to the output stream of the response.

This is my code:

...
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(jsonString);
out.flush();

I'm not sure how to handle this on the client side as it just displays the string on the page. What needs to be done?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
chris
  • 11

1 Answers1

3

A servlet returning a JSON is not meant to be called directly by the browser. It's meant to be called with JavaScript or another artifact that can interpret JSON.

Usually you will have something like:

var myObject = JSON.parse(myJSONtext, reviver);

That will get you an object parsed from JSON contents you send from servlet.

To get myJSONtext you usually do an AJAX call within a piece of Java Script code.

Google for: json ajax example

You will get a lot of information online.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292