Asynchronous requests can be implemented using XMLHttpRequest Browser objects (IE5, ActiveXObject objects in IE6), and Ajax is the encapsulation that is based on this.
1, synchronous and asynchronous:
<script type= "Text/javascript" >varXMLHTTP; functionloadxmldoc (URL, isasyn) {if(Window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, SafariXMLHTTP =NewXMLHttpRequest (); } Else{//code for IE6, IE5XMLHTTP =NewActiveXObject ("Microsoft.XMLHTTP"); } if(Isasyn) {//AsynchronousXmlhttp.open ("GET", URL,true); Xmlhttp.send (); } Else{//SynchronousXmlhttp.open ("GET", URL,false); Xmlhttp.send (); MyFunc (); } } functionMyFunc () {if(Xmlhttp.readystate = = 4 && xmlhttp.status = 200) {document.getElementById ("Mydiv"). InnerHTML =Xmlhttp.responsetext; } } </script> use: <button type= "button" onclick= "Loadxmldoc (' Lesson1.txt ', false)" > Change content via AJAX </button >
2. Get and Post:
Get: parameter in URL, send () parameter is emptyif(Isasyn) {//AsynchronousXmlhttp.open ("GET", URL,true); Xmlhttp.send ();} Else{//SynchronousXmlhttp.open ("GET", URL,false); Xmlhttp.send (); MyFunc ();}
Post: Parameters in the message "If you need to POST data like an HTML form, use setRequestHeader () to add an HTTP header. Then specify the data you want to send in the Send () method: "if(Isasyn) {//AsynchronousXmlhttp.open ("POST", URL,true); Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xmlhttp.send ("Fname=bill&lname=gates");} Else{//SynchronousXmlhttp.open ("POST", URL,false); Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xmlhttp.send ("Fname=bill&lname=gates");}
Use of the XMLHttpRequest object in the browser