Ajax Small Demo-----The use of JSON in Ajax

Source: Internet
Author: User

Using a simple example, the input of the form, the form input in the form of JSON, and back in the background JSON format, using the JS function processing, display, further familiar with the use of Ajax, as well as JSON usage. Examples are as follows:

HTML section: <form action= "test1.php"  method= "Get"  onsubmit= "Return check ();" > <label for= "username" > User name &nbsp;</label><input type= "text"   Placeholder= "Please enter user name"  id= "username" ><br><label for= "usernum" >&nbsp; study number &nbsp; </label><input type= "text"  placeholder= "Please enter the study number"  id= "Usernum"  onblur= "Checkform ();" ><br>    <input type= "Submit"  value= "Submission" >   < Div id= "Tips" ></div> </form> js part: <script>var xmlobj;var  Result=false;function createxmlhttprequest () {if (window. ActiveXObject) {                 xmlobj=new activexobject ("Microsoft.XMLHTTP");} Else if (window. XMLHttpRequest) {xmlobj=new xmlhttprequest ();}} Function check () {if (Resultform ()) {return true;} Else{return faLSE;}}     function checkform () {             var usernameobj=document.getelementbyid (' username '). value;var usernumobj= document.getElementById (' Usernum ') .value;var data={username:usernameobj,usernum:usernumobj};var  Jsonobj=json.stringify (data);//Convert object to JSON string, pass through Ajax              var cb = ajaxresultdeal; url= ' test.php?data= ' +jsonobj+ "&r=" + Math.random ();              toajax (URL,CB);       }    function toajax (Url,callback) {             createxmlhttprequest ();     Xmlobj.onreadystatechange=function () {if (xmlobj.readystate==4&&xmlobj.status==200) { callback ( Xmlobj.responsetext);     &nBsp;              }else{result=false;}}  xmlobj.open ("GET", url,true);/*xmlobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded" );  xmlobj.send (data); */xmlobj.send (null);} Function ajaxresultdeal (response) {               var tips=document.getelementbyid (' tips ');   var json=json.parse (response) ;     if (json[' username ']== "one") {      tips.innerhtml= "

When the user name is 11 o'clock, enter the correct

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7F/A3/wKioL1cnINLS--nHAAAuceDgEdc809.jpg "title=" 1.jpg " alt= "Wkiol1cninls--nhaaaucedgedc809.jpg"/>

When the user name is not 11 o'clock, the output is:

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/7F/A5/wKiom1cnICWw2krtAAAtokV0QVI383.jpg "title=" 2.jpg " alt= "Wkiom1cnicww2krtaaatokv0qvi383.jpg"/>

This article is from the "dream need to adhere to" blog, please be sure to keep this source http://xiyin001.blog.51cto.com/9831864/1769505

Ajax Small Demo-----The use of JSON in Ajax

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.