<input type= "button" Name= "btn" id= "btn" value= "button"/>
Js
Click the button to eject the 1.txt content
var Obtn=document.getelementbyid ("btn"); Obtn.onclick=function () {//Open Browser/* 1. Create an Ajax object IE6 the following new ActiveXObject (' Microsoft.XMLHTTP ') * */var xhr=null;try{xhr=new XMLHttpRequest ();} catch (e) {xhr=new activexobject (' Microsoft.XMLHTTP ');} In the Address bar, enter the address/*open method parameter 1. Open with 2. Address 3. Asynchronous Async: The code in front of nonblocking mode does not affect the execution synchronization of subsequent code: the code in front of blocking mode affects the execution of subsequent code * */
Asynchronous request Xhr.open (' Get ', ' 1.txt ', true);//Submit Send request Xhr.send ();//wait for the server to return content/*readystate:ajax work status request status * 0 (Initialize) No call to open () method * 1 (load) already called the Send () method, sending request * 2 (loading complete) the Send () method completed, received full response content * 3 (resolution) is parsing the response content * 4 (completion) Response content resolution completed, can be called on the client * STA Tus property: The state of the server (request Resource), the content returned by the HTTP status code responsetext: Returns content stored as text responsexml: Returns the content in XML form */xhr.onreadystatechange= function () {if (xhr.readystate==4) {if (xhr.status==200) {alert (xhr.responsetext);} Else{alert (' Error, ERR ' +xhr.status);}}}
JS Ajax Request Flow