Ajax 跨域访问


前台(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

===========================================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值