Asynchronous Object Five Section
This is the POST request,
//1.00 Creating an Asynchronous Object varXHR =NewXMLHttpRequest (); //2.0Xhr.open ("Post", Url,params,true); //3.0 passing parameters using the Formdata propertyXhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); //4.0 Setting the callback functionXhr.onreadystatechange =function () { if(Xhr.readystate = = 4 && xhr.status = 200) {alert (xhr.responsetext); } } //5.0 Passing ParametersXhr.send (params);
Encapsulates an asynchronous object with a GET request
In the GET request
Xhr.setrequestheader ("If-modified-since", "0"); is to clear the cache
While the POST request
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
It's for transmission.
At <from method= ' post ' type= "" >
The type in <from> can be obtained in three ways, including application/x-www-form-urlencoded
varAjaxhelp ={creatxhr:function () { //Creating an Asynchronous object varXHR =NewXMLHttpRequest (); returnXHR; }, //GET requests for AjaxAjaxget:function(URL, callBack) { This. Ajaxcommon ("Get", url,NULL, CallBack); }, //POST request for AjaxAjaxpost:function(URL, params, callBack) { This. Ajaxcommon ("POST", URL, params, callBack); }, Ajaxcommon:function(method, URL, params, callBack) {//1.0 varXHR = This. CREATXHR (); //2.0Xhr.open (method, URL,true); //3.0 if(method = = "Get") {Xhr.setrequestheader ("If-modified-since", "0"); } Else{Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); } //4.0Xhr.onreadystatechange =function () { if(Xhr.readystate = = 4 && xhr.status = 200) { varDatas =Json.parse (Xhr.responsetext); //Execute callback functionCallBack (datas); } } //5.0xhr.send (params); }};
PS: In jquery there are $.ajax and $.get/$. The method of the asynchronous request such as Post. The previous package will not be used. Amount It's a good rip. In fact, they are the bottom of the same writing. jquery is designed to solve the compatibility problems of each browser.
Help scripts for asynchronous objects (XMLHttpRequest)