ajax提交非同步驗證

來源:互聯網
上載者:User

簡單實用的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);
        }


注:關鍵代碼紅色標註

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.