The request method under Ajax and synchronous asynchronous difference summary _ajax related

Source: Internet
Author: User
Tags http request
Request method, divided into get and post:
Get
The most common HTTP request, the normal Internet browsing page is get. The parameter request for Get mode is directly followed by the URL and begins with a question mark. (JS used Window.location.search to get). Parameters can be encoded using encodeuricomponent, in the form of:
var enparam = encodeuricomponent (param);
URLs support only about 2K of length, that is, 2048 characters; using get for AJAX requests caches the resulting page is not correct, the general method is random parameter value; ajax.send (null).
POST
Submitting data to the server is used.
You need to remove the values from form forms into strings, connect with & symbols (same as get pass parameters), submit data 2GB, use Ajax.setrequestheader (' Content-type ', ' application/ X-www-form-urlencoded '), processing the submitted string, Ajax.send (strings), which represents what needs to be committed in the form, such as a=1&b=2 a string like this.
Synchronous and asynchronous:
In the Ajax.open method, the 3rd parameter is set either synchronously or asynchronously. Prototype, such as JS class library generally default to asynchronous, that is set to true. First of all, in the case of synchronization, JS will wait for the request to return to get status. The onReadyStateChange event handler function is not required. Asynchronous, however, requires onreadystatechange event handling and a value of 4 to properly handle the content below.
(Note: The Ajax in this article represents the XMLHTTP request object.) )
Copy Code code as follows:

Synchronous transfer Mode
function Requestbyget (nproducttemp,ncountrytemp)
{
var xmlhttp
if (window. XMLHttpRequest)
{
Isie = false;
XMLHTTP = new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
Isie = true;
XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
}
Web page location.
var url= "http://www.baidu.com/;
Xmlhttp.open ("Get", URL, false);
xmlHTTP. setRequestHeader ("Content-type", "text/html; Charset=shift_jis")
Xmlhttp.send (NULL);
var result = Xmlhttp.status;
Ok
if (result==200)
{
document.getElementById ("Div_rightbarbody"). Innerhtml=xmlhttp.responsetext;
}
XMLHTTP = null;
}

Asynchronous Transfer Mode
var xmlhttp
function Requestbyget (nproducttemp,ncountrytemp)
{
if (window. XMLHttpRequest)
{
Isie = false;
XMLHTTP = new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
Isie = true;
XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
}
Web page location.
var url= "http://www.baidu.com/";
Xmlhttp.open ("Get", URL, True);
Xmlhttp.onreadystatechange = Handleresponse;
xmlHTTP. setRequestHeader ("Content-type", "text/html; Charset=utf-8")
Xmlhttp.send (NULL);
}
function Handleresponse ()
{
if (xmlhttp.readystate = = 4 && xmlhttp.status==200)
{
document.getElementById ("Div_rightbarbody"). Innerhtml=xmlhttp.responsetext;
XMLHTTP = null;
}
}
Related Article

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.