Hello, I ran into a problem: I have a button, by clicking on which it starts to act, this AJAX sends a variable to the servlet (indicating what information to get), a json is formed on the servlet based on the received variable ------- and then I I just want to transfer this json object back to Ajax to read the received data. How to do it ? here is ajax:

$.ajax({ type: 'POST', data: $.toJSON(e.DId),//$.toJSON({'name':e.DId}), content: "UTF-8", contentType: 'application/json', url: "http://localhost:8080/MyDiplomka/ServWTest", success: function (data) { alert(data); // здесь хочу получить данные с сервлета } }); 

here is the servlet:

 Query query1 = em.createQuery("SELECT e FROM Questions e where e.DId=" + wq); List<Questions> arrQ = (List<Questions>) query1.getResultList(); for (Questions i : arrQ) { Query query2 = em.createQuery("select e from Answers e where e.QId=" + i.getQId()); List<Answers> arrA = (List<Answers>) query2.getResultList(); JSONArray jarr = new JSONArray(); for (Answers j : arrA) { JSONObject obj = new JSONObject(); obj.put("AContent", j.getAContent()); jarr.add(obj); } mainObj.put(i.getQContent(), jarr); } 

It turns out json object is already formed, but how to send it back to Ajax (where I want to get the object)? Thank you in advance. Ps: i use json-simle library to create json object

Update

Now

jarr.writeJSONString (response.getWriter ());

after that, [Object object] comes to me on AYAX - it looks like now you just need to somehow extract the resulting one.

    1 answer 1

    If I understood the question correctly, then you need something like the following: jarr.writeJSONString(response.getWriter());

    Where response is HttpServletResponse , which is the input parameter to the servlet