Solve the Ajax callback function no return is worth the problem

Source: Internet
Author: User

Here is an example of editing a validation role name:

First, define a flag global variable.

Then, in the callback function resp (), set the value of flag to true or false in the judgment.

Finally, in the Call Function Ckrname (), return flag is OK.

The code is as follows:

1 varXMLHTTP;2         functionCkrname () {3             varRoleName = document.getElementById ("RoleName"). Value; 4             varOldrolename = document.getElementById ("Oldrolename"). Value; 5             if(rolename== "" | | rolename==NULL||reg.test (rolename)) {6document.getElementById ("Sprname"). style.color= "Red";7document.getElementById ("Sprname"). innerhtml= "role name cannot be empty";8                 return false;9}Else{TenXMLHTTP =NewXMLHttpRequest (); One                 //Binding callback function status code change event AXmlhttp.onreadystatechange=resp; -                 //Send Request Preparation ("Get", "<%=basepath%>roleservlet?method=neweditrole&rolename=" +rolename+ "& Oldrolename= "+oldrolename,true); theXmlhttp.send (NULL); -                 returnFlag; -             } -         } + varFlag; -         functionresp () { +             if(xmlhttp.readystate==4&&xmlhttp.status==200){ A                 varTXT =Xmlhttp.responsetext; at                 if(txt>0){ -                     //alert ("Username already exists, please re-enter!") "); -document.getElementById ("Sprname"). style.color= "Red"; -document.getElementById ("Sprname"). innerhtml= "role name already exists, please re-enter! "; -flag=false; -}Else{ indocument.getElementById ("Sprname"). InnerHTML = "√"; -document.getElementById ("Sprname"). Style.color = "Green"; toflag=true; +                 } -             } the}

Solve the Ajax callback function no return is worth the problem

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: 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.