Use the return value after success in Ajax

Source: Internet
Author: User
Tags button type
function Checkform () {var pass;
        var msg;
        var url= "${ctx}/crm/contact.action?method:checkform";
        var Qq=document.getelementbyid ("Contact_qq"). Value;
        var Mobile=document.getelementbyid ("Contact_mobile"). Value;
        var Email=document.getelementbyid ("Contact_email"). Value;
            $.ajax ({url:url, async:false, type: "Post", DataType: "JSON",
                Data: {qq:qq, Mobile:mobile, Email:email
                }, Success:function (data) {pass=data.pass;
                msg=data.msg; if (pass) {alert ("Save succeeded.
                ");
                }else{alert (msg);
        }
            }
      });
        if (pass) return true;

    else return false; } <button type= "Submit" class= "btn btn-sm btn-primary savabtn" Name= "metHod:save "onclick=" return Checkform () "> Save </button>
 

The

Important step is async:false, because Ajax is asynchronous by default, that is, the default is Async:true. When set to False, the pass value returned to the foreground in Ajax can be used in the function Checkform.
in action,

public void Checkform () {string mobile= (string) getparameter ("mobile", String.class);
        String email= (String) getparameter ("email", string.class);

        String qq= (String) getparameter ("QQ", String.class);
        Boolean pass=true;
        List mobilelist = Contactmanager.find ("From Contact where mobile=?", mobile);
        List emaillist = Contactmanager.find ("From Contact where email=?", email);
        List qqlist = Contactmanager.find ("From Contact where qq=?", QQ); Mobile is not empty and size is not 0, that is, duplicate if ((! Mobile ==null | |
            Mobile.equals (""))) && mobilelist.size ()!=0) {pass=false;          
        msg+= "mobile phone Number:" +mobile+ "repeat"; } if ((!) ( Email ==null | |
            Email.equals (""))) && emaillist.size ()!=0) {pass=false;          
        msg+= "Email:" +email+ "repeat"; } if ((!) ( QQ ==null | |
            Qq.equals (""))) && qqlist.size ()!=0) {pass=false;    
  msg+= "QQ number:" +qq+ "repeat";      } jsonobject resultobject = new Jsonobject ();
        Resultobject.put ("Pass", pass); if (! ( msg ==null | |
        Msg.equals (""))) {Resultobject.put ("msg", Msg.substring (4));
        }else{resultobject.put ("msg", MSG);
        } System.out.println ("==========");
        SYSTEM.OUT.PRINTLN ("pass" +pass);
        System.out.println (Resultobject.tostring ());
    Responsewrite (Resultobject.tostring ()); }
Related Article

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.