Write casually:
An asynchronous request, which is the meaning of a partial refresh, which is simply a partial request sent to the browser.
There are two major categories of requests divided into get:post.
Self-encapsulated Ajax
var ajaxhelp = {
Returns the created asynchronous object
Getreques:function () {
var xlm = new XMLHttpRequest ();
return XLM;
},
Asynchronous object request//Request mode, address, parameter, callback function
Processreques:function (methor, URL, params, callblack) {
var XLR = this. Getreques ();
if (Methor = = "Get")
{
Open channel parameters are: Request method, address, whether asynchronous
Xlr.open ("Get", url, True);
Set Request Header//Clear browser cache time, set to 0
Xlr.setrequestheader ("If-modified-since", "0");
}
Else
{
Xlr.open ("Post", url, True);
Set the request header//Set the encoding format, default is the form inside the format
Xlr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
}
Change of request state triggered
Xlr.onreadystatechange = function () {
When the request status is 4 and the Send status is 200
if (xlr.readystate = = 4 | | xlr.stute = = 200) {
Get the data returned by the browser
var objecs = Json.parse (Xlr.responsetext);
if (objecs. Statu = = 0)//Determine whether the status of the currently returned data is successful
{
Execute callback function
Callblack (objecs. data);//Return to the asynchronous image
}
else {callblack (objecs). MGS); }//returns the prompt data for an asynchronous object
}
}
XLR (params);
},
Get method Request
Get:function (Url,params,callback)
{
This. Processreques ("Get", url, params, CallBack);
},
Post-mode request
Post:function (Url,params,callback)
{
This. Processreques ("Post", url, params, CallBack);
}
}
Ajax asynchronous requests