Turn from: HTTPS://WWW.CNBLOGS.COM/YUFANN/P/JS-AJAX.HTML1. Creating an Ajax Object
Non-IE6 browser: var obj = new Xmlhttpreuqest ();
IE6 browser: var obj = new Activexbject ("Microsoft.XMLHTTP");
2. Connect to the server
Obj.open (Request method, URL, whether asynchronous)
How to block caching:
Obj.open (' Get ', ' yan.txt?name= ' +new Date (). GetTime (), true);
3. Sending the request
Obj.send ();
4. Receive return value
Request Status Monitoring: onReadyStateChange event: Triggers when there is communication between Ajax and the server
Mainly through the ReadyState attribute to determine whether there is no end, the end is also not successful, the status attribute to judge
1.----ReadyState Properties: Request Status
0 (uninitialized) has not yet called the Send () method
1 (loaded) has called the Send () method and is sending the request
2 (loading complete) the Send () method executes and has received the full response content
3 (interactive) parsing response content
4 (complete) Response content resolution completed, can be called at the client (completion is not necessarily successful, need status to detect success)
2.---Status property: Request result, success (200) or failure (404): obj.status==200
3.---return value responsetext: Text returned from the server: Obj.responsetext (returned as a string)
Get mode
functionGetajax () {varobj; if(ActiveXObject)//Judging whether it's IE6obj =NewActiveXObject ("Microsoft.XMLHTTP"); Elseobj=NewXMLHttpRequest (); //connecting to a serverObj.open (' Get ', ' http://localhost:8080/yan.ashx?name=123 ',true); //Send RequestObj.send (); Obj.onreadystatechange=function(){ if(obj.readystate===4&&obj.status===200)//If the receive is complete and the request succeedsConsole.log (Obj.responsetext);//Output return information } }
Post mode
functionPostajax () {varobj; if(ActiveXObject)//Judging whether it's IE6obj =NewActiveXObject (' Microsoft.XMLHTTP '); Elseobj=NewXMLHttpRequest (); //connecting to a serverObj.open (' Post ', ' http://localhost:8080/yan.ashx?name=123 ',true); //Set Header informationObj.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); //send request, pass dataObj.send ({name: ' 123 ')}); Obj.onreadystatechange=function(){ if(obj.readystate===4&&obj.status===200)//If the receive is complete and the request succeedsConsole.log (Obj.responsetext);//Output return information } }
Native JS Ajax