First say the basics of Get type Ajax
functionLoadxmldoc () {varXMLHTTP;
First determine if the browser supports XMLHttpRequest, because ie56 is not the object, it is ActiveXObjectif(window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safarixmlhttp=NewXMLHttpRequest (); }Else {//code for IE6, IE5xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); }
Then, when the readystate changes, the judge state is normal, if the normal description of the request is completed, the request after the operation Xmlhttp.onreadystatechange=function() { if(Xmlhttp.readystate==4 && xmlhttp.status==200) {document.getElementById ("Mydiv"). innerhtml=Xmlhttp.responsetext; } }
Configuration parameter Xmlhttp.open ("GET", "/ajax/demo_get.asp",true);
Submit Request Xmlhttp.send ();
Here is the post type, the difference is not big
varXMLHTTP;if(window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safarixmlhttp=NewXMLHttpRequest (); }Else {//code for IE6, IE5xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); }xmlhttp.onreadystatechange=function() { if(Xmlhttp.readystate==4 && xmlhttp.status==200) {document.getElementById ("Mydiv"). innerhtml=Xmlhttp.responsetext; }}xmlhttp.open ("POST", "http://www.weisuyun.com/xiaochengxu.php",true);//get changed to Postxmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");//This is the header that adds HTTP xmlhttp.send ("Name=bill&sex=gates");//send inside Write parameters
And finally, a little bit of integration.
<HTML><Head><Scripttype= "Text/javascript">varXMLHTTP;functionLoadxmldoc (url,cfunc) {if(window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, SafariXMLHTTP=NewXMLHttpRequest (); }Else {//code for IE6, IE5XMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); }xmlhttp.onreadystatechange=Cfunc;xmlhttp.open ("GET", URLs,true); Xmlhttp.send ();}functionmyFunction () {Loadxmldoc ("/ajax/test1.txt",function() { if(Xmlhttp.readystate==4 &&Xmlhttp.status== $) {document.getElementById ("mydiv"). InnerHTML=Xmlhttp.responsetext; } });}</Script></Head><Body><DivID= "Mydiv"><H2>Let AJAX change this text</H2></Div><Buttontype= "button"onclick= "myFunction ()">Change content with AJAX</Button></Body></HTML>
The Ajax of PHP JavaScript