//
$.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”,所以返回值是要处理一下的,至于
怎么处理 现在还不清楚
cardInfo.score="0";
}
if(cardInfo.discount==''){
cardInfo.discount="100";
}
$("#cardInfoDetail").html("剩余积分:<font color='blue'> "+cardInfo.score+" </font>分 折扣:<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();
}
}