现在的位置: 首页 > 综合 > 正文

ajax提交异步验证

2018年04月04日 ⁄ 综合 ⁄ 共 3665字 ⁄ 字号 评论关闭

简单实用的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);

        }


注:关键代码红色标注

   

抱歉!评论已关闭.