Home » Java » javascript – how to return the json formatted string back to ajax call?-Exceptionshub

javascript – how to return the json formatted string back to ajax call?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m sending the request through Ajax to a JSP file from a JSP file with the following code.

 $.ajax({
                            type:"POST",
                            url:"reviewername.jsp",
                            data : "vendorgroupid="+$('#vendorgroup').val(),
                            cache:false,
                           headers: {Accept : "application/json;charset=utf-8"},

                            sucess:function(data)
                            {
                                console.log('Hello World!');
                                console.log(data)
                                console.log('Hello World!');
                                $(".reviewusername").html($.trim(data));                    
                            }
                        })

from the request I’m making some operation and trying to send it back with the folliwng code

List<String> listven = new ArrayList<>();
    VendorDAO vd=new VendorDAO();
    String vendorgroupid = request.getParameter("vendorgroupid");
    System.out.println(vendorgroupid);
    listven=vd.getReviewerUsername1(vendorgroupid);
    JSONObject json = new JSONObject();
     for (int i = 0; i < listven.size(); i++) {
         json.put("name", listven.get(i));
     }
     JSONArray array = new JSONArray();
     array.put(json);
     System.out.println(array.toString());
     out.print(array.toString());
     out.flush();
     //response.g

After that I’ll get the response on sucess and will show it as drop-down to user.

But I can’t send back data to the ajax call, how to do it?

How to&Answers: