源 JSP:
String r_payment_code = request.getParameter("paymentcode");
String sql = "SELECT hd.invoice_id, tt.payment_code"
+" FROM dbt_invoice hd, dbt_payment tt"
+" WHERE hd.payment_id = tt.payment_id"
+" AND hd.period_id = 147"
+" AND tt.period_id = 147"
+" AND tt.payment_code LIKE 'NDH"+ r_payment_code +"%' and rownum <=5";
RecordSet sData = DBUtil.executeQuery(db_name, sql);
JSONObject json = new JSONObject();
JSONArray jsonarr = new JSONArray();
JSONObject jsonobj;
int i = 0;
String[] arrPaymentCode = new String[sData.RecordCount];
while(sData.next()){
jsonobj = new JSONObject();
jsonobj.put("invoice_id", sData.getString("invoice_id"));
jsonobj.put("payment_code", sData.getString("payment_code"));
jsonarr.add(jsonobj);
}
json.put("SRC_PAYMENCODE", jsonarr);
response.setContentType("application/json");
out.println(json);
从 JSP 页面输出:
[{"invoice_id":"6384","payment_code":"NDH000228015"},{"invoice_id":"6385","payment_code":"NDH000230254"},{"invoice_id":"6386","payment_code": "NDH000228302"},{"invoice_id":"6387","payment_code":"NDH000228303"},{"invoice_id":"6388","payment_code":"NDH000228311"}]
从 HTML 调用
$("#txtPaymentCode").autocomplete(
$.ajax({
type: "POST",
url: "../../common/script/jquery/autocomplete/srcData.jsp",
data :{paymentcode : inputString},
dataType: "json",
success: function(data){
data = JSON.parse(data);
alert(data)
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
document.getElementById("dvShowSearchPhone").innerHTML = thrownError;
}
})
);
无效的 JSON:[{"invoice_id":"6384","payment_code":"NDH000228015"},{"invoice_id":"6385","payment_code":"NDH000230254"},{"invoice_id":"6386"," payment_code":"NDH000228302"},{"invoice_id":"6387","payment_code":"NDH000228303"},{"invoice_id":"6388","payment_code":"NDH000228311"}]