从serlvet得到回应json

时间:2017-01-05 11:11:53

标签: javascript java json servlets

如何从servlet接收JSON响应到javascript?我使用AJAX post

将它传递给servlet
 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);
            }
        });

这是我的servlet代码

 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());
}

这是我的回应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"
}]}

我已经多次尝试但是我没有成功。请帮忙!

1 个答案:

答案 0 :(得分:0)

首先设置内容类型,然后按如下方式初始化您的printwriter对象:

PrintWriter out = null;     
res.setContentType("application/json; charset=UTF-8");
out = res.getWriter();