0

源 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"}]

4

2 回答 2

0

不完全确定问题是什么,但我猜。
如果您尝试自动完成支付代码,当您在 javascript 回调处理程序中尝试 data.payment_code 时会发生什么(即

success: function(data){
data = JSON.parse(data);
alert(data.payment_code)
},
于 2012-06-26T11:22:42.477 回答
0

不确定,只是猜测

尝试用分号结束您的 ajax 调用 >> }) 应该是 });

并尝试添加

            contentType: "application/json; charset=utf-8",
于 2012-06-26T11:49:12.013 回答