六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 32|回复: 0

Ajax 跨域访问

[复制链接]

升级  84%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
42
 楼主| 发表于 2013-1-29 10:31:30 | 显示全部楼层 |阅读模式
前台(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

===========================================================================
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表