Ajax asynchronous requests

Source: Internet
Author: User
Tags browser cache clear browser cache

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.