Simple and practical ajax asynchronous Verification Code
Jqueyr code:
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 ("You cannot quote yourself !! ");
} 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 ("submission failed! ");
} Else {
If (codes. code = "1 "){
Alert ("the quotation is submitted successfully! ");
If (confirm ("Click OK to refresh. The delegated purchase page is displayed! ")){
Document. location. href = "$! WebPath/itTraPbillInfo/list.htm ";
}
} Else {
If (codes. code = "2 "){
Document. location. href = "$! WebPath/login.htm ";
}
}
}
}
});
}
}
Note: $! WebPath is pre-defined, http: // localhost: 8080/Project Name
Control Layer:
@ 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 incluiseid = enterprise. getId ();
TTraUser user = SecurityUserHolder. getCurrentUser ();
String userId = user. getId ();
TTraPprice traPprice = new TTraPprice ();
TraPprice. setincluiseid (incluiseid );
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 );
}
Note: key codes are marked in red.