Ajax submits asynchronous Verification

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.