0

How do I receive a JSON response from servlet to javascript ? I'm passing it to the servlet using AJAX post

 this my ajax code

 $.ajax({
            type: "POST",
            url: "formDelegationServlet",
            data: {
                jsonfield: JSON.stringify(dataString) // look here!
            },
            dataType: "json",
            //if received a response from the server
            success: function (response) {
                //our country code was correct so we have some information to display
                if (response.success === true) {
                    console.log("response" + response);
                    $("#kota_merchant").val(response.rows.kota_merchant_del);
                    $("#alamat_merchant").val(response.rows.alamat_merchant_del);
                    $("#province_merchant").append(response.rows.prov_merchant_del);

                }
                //display error message
                else {
                    $("#ajaxResponse").html("<div><b>Merchant Name in Invalid!</b></div>");
                }
            },
            //If there was no resonse from the server
            error: function (jqXHR, textStatus, errorThrown) {
                console.log("Something really bad happened " + textStatus);
                $("#ajaxResponse").html(jqXHR.responseText);
            }
        });

this my servlet code

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    JSONArray array = new JSONArray();
                for (int i = 0; i < dataMerchant.size(); i++) {
                    JSONObject obj = new JSONObject();
                    EntityMerchant entityMerchant = dataMerchant.get(i);

                    if (entityMerchant.getNamaMerchant() == null) {
                        obj.put("nama_merchant_del", "");
                        obj.put("success", false);
                    } else {
                        obj.put("nama_merchant_del", entityMerchant.getNamaMerchant());
                        obj.put("success", true);
                    }
                    if (entityMerchant.getAlamatMerchant() == null) {
                        obj.put("alamat_merchant_del", "");
                    } else {
                        obj.put("alamat_merchant_del", entityMerchant.getAlamatMerchant());
                    }
                    if (entityMerchant.getKota() == null) {
                        obj.put("kota_merchant_del", "");
                    } else {
                        obj.put("kota_merchant_del", entityMerchant.getKota());
                    }
                    if (entityMerchant.getProvinsi() == null) {
                        obj.put("prov_merchant_del", "");
                    } else {
                        obj.put("prov_merchant_del", entityMerchant.getProvinsi());
                    }
                    if (entityMerchant.getPassword() == null) {
                        obj.put("pas_merchant_del", "");
                    } else {
                        obj.put("pas_merchant_del", entityMerchant.getPassword());
                    }
                    array.add(obj);

                }
                em.close();
                JSONObject jsonobj = new JSONObject();
                jsonobj.put("rows", array);
                out.print(jsonobj.toString());
}

this my response json

{"rows":[{"nama_merchant_del":"MAJU SUKSES OCEAN","success":true,"alamat_merchant_del":"JL. DIPONEGORO
 NO.  1B","kota_merchant_del":"JAKARTA PUSAT","prov_merchant_del":"DKI JAKARTA","pas_merchant_del":"0"
}]}

I have tried it many times but I've been unsuccessful. Please help!

newbiecihuy
  • 135
  • 1
  • 3
  • 12

1 Answers1

0

First set content type and then Initialize your printwriter object as below:

PrintWriter out = null;     
res.setContentType("application/json; charset=UTF-8");
out = res.getWriter();
Nimesh
  • 794
  • 3
  • 8
  • 18