简单实用的ajax异步校验代码
jqueyr代码:
function submitPriceInfo(){ var price = $("#price").val(); var amount = $("#amount").val(); var pbillId = $("#pbillId").val(); var curentId = "$!enterprise.id"; var billId = "$!tTraPbill.enterpirse.id"; if(curentId==billId){ alert("不能给自己报价!!"); }else{ $.ajax({ type : "post", url : "$!webPath/usercenter/delegateManage/submitPprice.htm", data : {price:price,amount:amount,pbillId:pbillId}, async : false, success : function(data){ var codes = jQuery.parseJSON(data); if(codes.code=="0"){ alert("提交不成功!"); }else{ if(codes.code=="1"){ alert("报价提交成功!"); if(confirm("点击确定刷新后将显示委托采购页面!")){ document.location.href="$!webPath/itTraPbillInfo/list.htm"; } }else{ if(codes.code="2"){ document.location.href="$!webPath/login.htm"; } } } } }); }
}
注:$!webPath为事先定义好的,为http://localhost:8080/项目名
控制层:
@RequestMapping("/submitPprice.htm") @ResponseBody public ResponseEntity<String> submitPprice(HttpServletRequest request, HttpServletResponse response) { String json = ""; String price = request.getParameter("price"); String amount = request.getParameter("amount"); String pbillId = request.getParameter("pbillId"); TTraEnterprise enterprise = SecurityUserHolder.getCurrentTTraEnterprise(); if(enterprise==null){ json = "{"code":"2"}"; }else{ String enterpriseId = enterprise.getId(); TTraUser user = SecurityUserHolder.getCurrentUser(); String userId = user.getId(); TTraPprice traPprice = new TTraPprice(); traPprice.setEnterpriseId(enterpriseId); traPprice.setPrice(Double.parseDouble(price)); traPprice.setAmount(Double.parseDouble(amount)); traPprice.setPbillId(pbillId); traPprice.setAddUserId(userId); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = sdf.format(new Date()); Date date = null; try { date = sdf.parse(dateStr); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } traPprice.setAddTime(date); traPprice.setStateFlag("0"); TTraPbill ttraPbill = itTraPbillService.getObjById(pbillId); if(itTraPpriceService.save(traPprice)){ json = "{"code":"1"}"; if(ttraPbill.getStateFlag().equals("0")){ ttraPbill.setStateFlag("2"); } if(ttraPbill.getApriceNum()==null || ttraPbill.getApriceNum()==0){ ttraPbill.setApriceNum(1l); }else{ ttraPbill.setApriceNum(ttraPbill.getApriceNum()+1); } itTraPbillService.update(ttraPbill); }else{ json = "{"code":"0"}"; } } HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type","application/text;charset=UTF-8"); return new ResponseEntity<String>(json,headers,HttpStatus.ACCEPTED); }
注:关键代码红色标注 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|