How to request in Ajax and the difference between synchronous and asynchronous

Source: Internet
Author: User

Request method, divided into get and post:GETThe most common HTTP request, the normal Internet browsing page is get. The get mode parameter request is immediately followed by the URL, starting with a question mark. (JS is obtained with Window.location.search). Parameters can be encoded with encodeuricomponent, using the following methods:

var EnParam = encodeURIComponent(param);

The URL supports only about 2K of length, that is, 2048 characters; When Ajax requests using get are cached, the resulting page is not correct, and the general method adds the random parameter value; ajax.send (null).

POST

It is used to submit data to the server.

The values in the form form need to be taken out first to be converted to strings, concatenated with the & symbol (same as get arguments), commit data 2GB; using Ajax.setrequestheader (' Content-type ', ' application/ X-www-form-urlencoded '), which handles 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 vs. Asynchronous:

In the Ajax.open method, the 3rd parameter is set to synchronous or asynchronous. Prototype such as JS class library is 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, get status. You do not need the onReadyStateChange event handler function. Asynchronous, however, requires onreadystatechange event handling, and a value of 4 correctly handles the following.

(Note: Ajax in this article represents the XMLHTTP request object.) )

1//Synchronous transfer Mode
2
3functionRequestbyget (nproducttemp,ncountrytemp)
4{
5VarxmlHTTP
6
7If(Window. XMLHttpRequest)
8{
9//Isie = false;
10xmlHTTP=NewXMLHttpRequest ();
11}
12ElseIf(Window. ActiveXObject)
13{
14//Isie = true;
15xmlHTTP=NewActiveXObject ("Microsoft.XMLHTTP");
16}
17
18//Web page location.
19VarUrl="http://www.baidu.com/;
20Xmlhttp.open ("GET", URL, false);
21stxmlHTTP. setRequestHeader ("Content-Type","Text/html Charset=shift_jis ")
22Xmlhttp.send (Null);
23VarResult=Xmlhttp.status;
24
25//Ok
26If(Result==200)
27{
28document.getElementById ("Div_rightbarbody"). InnerHTML=Xmlhttp.responsetext;
29}
30xmlHTTP=Null;
31}
32
33
34//Asynchronous Transfer Mode
35VarxmlHTTP
36
37functionRequestbyget (nproducttemp,ncountrytemp)
38{
39If(Window. XMLHttpRequest)
40{
41 //Isie = false;
42xmlHTTP=NewXMLHttpRequest ();
43}
44ElseIf(Window. ActiveXObject)
45{
46//isie   =   true;  47           xmlhttp   =   new   activexobject ("Microsoft.XMLHTTP" );  48     }49                   50     //Web page  Location.51     var url= "http://www.baidu.com/";52      Xmlhttp.open ("GET", Url, true);53     xmlhttp.onreadystatechange =  Handleresponse;54     //xmlhttp. setRequestHeader ("Content-type", "Text/html; charset=utf-8") 55     xmlhttp.send ( NULL);   56 }57 58 function handleresponse () 59 {60      if (xmlhttp.readystate == 4 && xmlhttp.status==200) 61     {62          document.getElementById ("Div_rightbarbody") .innerhtml=xmlhttp.responsetext;63          xmlhttp = null;64     }65 }66 

How to request in Ajax and the difference between synchronous and asynchronous

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.