Servlet doesnt write JSON Output to AJAX

Multi tool use
Servlet doesnt write JSON Output to AJAX
Following code for my output:
PrintWriter out = response.getWriter();
ObjectMapper objectMapper = new ObjectMapper();
ToJson obj = new ToJson();
String obj1 = objectMapper.writeValueAsString(obj);
response.setContentType("application/json");
out.print(obj1);
System.out.println(obj1);
out.close();
The obj1 looks like this: {"prname1":"P1neu","anz1":"342356","prid1":"1","price1":"25"}
{"prname1":"P1neu","anz1":"342356","prid1":"1","price1":"25"}
It should send the string out so I can parse it in my AJAX and display it but somwhow it ends up with nothing as console.log/etc doesnt display any data.
I had out.append
but it also didnt work.
out.append
1 Answer
1
Use below code to send response as JSON.
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(obj1);
Please check this How to use Servlets and Ajax?
It will surely help you.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.