jquery的两种ajax请求

//

$.ajax({ url: "test.html", context: document.body, success: function(){
        $(this).addClass("done");
      }});

$.ajax({

            url:"cardAction_findVipByCardId.action",
            data:{"cardId":cardId},
            type:"POST",
            dataType:"text",
            success:function(result){                      //result就是从后台以json格式传回来的值,名字不重要
                if(result!="{}"){

                    cardInfo=eval("("+result+")");     

                   //eval("("+result")"),知道是js的类似计算的函数,可是不知道为什么要这样写,

                      是必要的吗?因为返回值的类型是“text”,所以返回值是要处理一下的,至于

                        怎么处理 现在还不清楚

                                                                                     

                    if(cardInfo.score==''){
                        cardInfo.score="0";
                    }
                    if(cardInfo.discount==''){
                        cardInfo.discount="100";
                    }
                    $("#cardInfoDetail").html("剩余积分:<font color='blue'> "+cardInfo.score+" </font>分 &nbsp;  &nbsp; 折扣:<font color='blue'>"+cardInfo.discount+"</font>折");
                    $("#all_score").val(cardInfo.score);
                  //  $("#js_tableDis").show();
                }else{
                    alert("您输入的会员卡号不存在,请确认后重新输入!")
                    $("#cardInfoDetail").html("");
                    //$("#js_tableDis").hide();
                }
                
            }

        });

后台:

/**
     * 先判断会员是否存在,然后根据会员卡查询会员信息,以JSON格式返回
     * @return
     */
    public void findVipByCardId(){
        PrintWriter pw=null;
        HttpServletResponse resp=response();
        Connection conn=null;
        try {
            conn=DbcpConnection.getConnection();
            resp.setCharacterEncoding("UTF-8");
            pw=resp.getWriter();
            User u=this.getCurrentUser();
            boolean flag = cardInfoService.checkCardIdExist(model.getCardId().trim(), u.getCompanyId(), conn);
            if(flag){
                model=cardInfoService.findCardInfo( model.getCardId().trim(),u.getCompanyId(),conn);
                pw.write(JsonUtil.parseToJSON(model));
            }else{
                pw.write("{}");
            }
        }catch (Exception e) {
            pw.write("{}");
            log.fatal("VioAction.findAllByCard出错",e);
        }finally{
            DbcpConnection.close(conn, null, null);
            pw.flush();
            pw.close();
        }
    }


//jquery.post(url,[data],[callback],[type])

$.post("cardAction_checkChargeScoreNum.action",
            {
                cardId:cardId,iptscore:iptscore
              },
            function(data,status){
                    if(status=="success"){
                        if(data.flag==0){
                            alert("您的积分值不够");
                            return false;
                            }
                        }
            });     

后台: 

public void checkChargeScoreNum(){
        Connection conn = null;
        PrintWriter out = null;
        HttpServletRequest request = this.request();
        HttpServletResponse response = this.response();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        User user = this.getCurrentUser();
        String cardId = request.getParameter("cardId");
        String iptScore = request.getParameter("iptscore");
        try{
            conn = DbcpConnection.getConnection();
            out = response.getWriter();
            int flag = cardInfoService.checkChargeScoreNum(cardId, user.getCompanyId(), iptScore, conn)?1:0;
            JSONObject json = new JSONObject();
            json.put("flag", flag);
            out.write(json.toString());
        }catch(Exception e){
            out.write("0");
            log.fatal("CardInfoAction.checkChargeScoreNum", e);
        }finally{
            DbcpConnection.close(conn, null, null);

           out.flush();

           out.close();

         }
        
    }  



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值