Explore the difference between synchronous and asynchronous in Ajax

Time:2021-8-1

The request mode is divided into get and post: get is the most common HTTP request. The ordinary Internet browsing page is get. The parameter request in get mode directly follows the URL and starts with a question mark( JS (obtained by window. Location. Search). Parameters can be encoded with encodeuricomponent. Usage:
var EnParam = encodeURIComponent(param);

The URL only supports a length of about 2K, that is, 2048 characters; When using get to make Ajax requests, the cache will lead to incorrect pages. The general method is to add the random parameter value; ajax.send(null)。

POST

Submit data to the server.

The value in the form needs to be taken out first, converted into a string, and connected with the & symbol (the same as the parameter passed by get); The amount of data submitted is 2GB; Use ajax. Setrequestheader (‘content type ‘,’ application / x-www-form-urlencoded ‘) to process the submitted string; Ajax. Send (strings), which represents the content to be submitted in the form, such as a = 1 & B = 2.

Synchronous and asynchronous:

In the Ajax. Open method, the third parameter is 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. The onreadystatechange event handler is not required. Asynchronous requires onreadystatechange event processing, with a value of 4, and then correctly handle the following contents.

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