前台(JQuery)
==========================================================================
function showAmount(){
$.ajax({
type: "GET",
url: "http://localhost:8080/sz/orderForm.do?method=queryAmountOfConsumptionByMobile",
data:{mobileNumber:'<%=mobile%>'},
dataType:"jsonp",
success: function(data){
for(var i=0;i<data.length;i++){
var amount = data[i].amount;
$('#amount').append("<b>"+amount+"</b>");
};
},
error: function(){
$('#amount').append("<b>无数据</b>");
}
});
}
==========================================================================
后台
=========================================================================
public void queryAmountOfConsumptionByMobile(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
Double amount = 0.0;
String mobile = request.getParameter("mobileNumber");
//根据状态用户的ID以及订单的状态查询出结果
try {
amount = orderFormInfoBO.getAmountOfConsumption(mobile);
}
catch (Exception ex) {
if (log.isDebugEnabled()) {
ex.printStackTrace();
}
log.error("查询个人消费总数的时候发生错误\n" + ex);
out.print("查询出错!");
}
//将金额格式化为小数点后两位精度
BigDecimal bd = new BigDecimal(amount);
BigDecimal bd1 = bd.setScale(2,bd.ROUND_HALF_UP);
String amountStr = bd1.toString();
String cb = request.getParameter("callback");//若果是ajax请求会带这个参数
JSONArray ja = new JSONArray();
JSONObject j = new JSONObject();
j.put("amount", amountStr);
ja.add(j);
if(cb != null){
StringBuffer sb = new StringBuffer(cb);
sb.append("(");
sb.append(ja.toString());
sb.append(")");
out.write(sb.toString());
out.close();
}else{
out.write(ja.toString());
out.close();
}
}
============================================================================
JSON所需包
==========================================================================
commons-beanutils.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1-rc1.jar
commons-lang.jar
ezmorph-1.0.4.jar
json-lib-2.2-jdk13.jar
===========================================================================
Ajax 跨域访问
最新推荐文章于 2024-07-21 17:13:47 发布