Differences between request methods and synchronous Asynchronization in Ajax

Source: Internet
Author: User
Request methods include get and post: Get, which are the most common HTTP requests. The GET request directly follows the URL and starts with a question mark. (Window. Location. Search is used in JS ). The parameters can be encoded using encodeuricomponent. Usage:

VaREnparam=Encodeuricomponent(Param);

The URL can only be about 2 k in length, that is, 2048 characters. When Ajax requests are made using get, the cached page may not appear correctly. Generally, the random parameter value is added. Ajax. send (null ).

Post

It is used to submit data to the server.

You need to extract and convert the values in the form into strings and connect them with the & Symbol (same as the get parameter). Submit 2 GB of data volume. Use Ajax. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded') to process the submitted string. Ajax. send (strings). This strings indicates the content to be submitted in form, for example, a = 1 & B = 2.

 

Synchronous and asynchronous:

In the Ajax. Open method, 3rd parameters are set to synchronous or asynchronous. JS class libraries such as prototype are generally asynchronous by default, that is, set to true. First, in the case of synchronization, JS will wait for the request to return and get the status. Onreadystatechange event handler is not required. While asynchronous processing requires onreadystatechange event processing, and the value is 4, and then the following content is processed correctly.

(Note: Ajax in the text indicates the XMLHTTP request object .)

 

 

1 // Synchronous Transmission Mode
2
3 Function Requestbyget (nproducttemp, ncountrytemp)
4 {
5 VaR XMLHTTP
6
7 If (Window. XMLHttpRequest)
8 {
9 // Isie = false;
10 XMLHTTP = New XMLHttpRequest ();
11 }
12 Else   If (Window. activexobject)
13 {
14 // Isie = true;
15 XMLHTTP = New Activexobject ( " Microsoft. XMLHTTP " );
16 }
17
18 // Web page location.
19 VaR URL = " Http://www.baidu.com /;
20 XMLHTTP. Open ( " Get " , URL, false );
21 // XMLHTTP. setRequestHeader ( " Content - Type " , " Text / HTML; charset = shift_jis ")
22 XMLHTTP. Send ( Null );
23 VaR Result = XMLHTTP. status;
24
25 // OK
26 If (Result = 200 )
27 {
28 Document. getelementbyid ( " Div_rightbarbody " ). Innerhtml = XMLHTTP. responsetext;
29 }
30 XMLHTTP =   Null ;
31 }
32
33
34 // Asynchronous transmission mode
35 VaR XMLHTTP
36
37 Function Requestbyget (nproducttemp, ncountrytemp)
38 {
39 If (Window. XMLHttpRequest)
40 {
41 // Isie = false;
42 XMLHTTP = New XMLHttpRequest ();
43 }
44 Else   If (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

 

 

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.