0
如何從servlet接收到JavaScript的JavaScript響應?我使用AJAX後把它傳遞給servlet得到迴應json from serlvet
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"
}]}
我已經嘗試過很多次,但我已經不成功。請幫忙!
可以還張貼你的servlet方法的簽名? – Ankit
什麼不工作? – ragelh