Preface
When Ajax is used, when we send data to the server, we can use the Get method to request the server or the Post method to request the server. so when should we adopt the Get method and the Post method?
Difference between Get request and Post request
1. When a Get request is used, the parameter is displayed in the URL, but the Post method is not displayed.
2. A small amount of data is sent using the Get request, and a large amount of data is sent using the Post request.
Example
HTML code of the page:
<Html xmlns = "http://www.w3.org/1999/xhtml">
Differences:
|
Get request |
Post request |
Client foot Ben Generation Code |
Function btn_get_click () {var xmlHttp = window. XMLHttpRequest? New XMLHttpRequest (): new ActiveXObject ("Microsoft. XMLHTTP "); var username = document. getElementById ("txt_username "). value; var age = document. getElementById ("txt_age "). value; // Add parameters to access different URLs each time to avoid cache problems xmlHttp. open ("get", "Server. aspx? Username = "+ encodeURIComponent (username) +" & age = "+ encodeURIComponent (age) +" & random = "+ Math. random (); xmlHttp. onreadystatechange = function () {if (xmlHttp. readyState = 4 & xmlHttp. status = 200) {document. getElementById ("result "). innerHTML = xmlHttp. responseText ;}// send the request. The parameter is null xmlHttp. send (null );} |
Function btn_post_click () {var xmlHttp = window. XMLHttpRequest? New XMLHttpRequest (): new ActiveXObject ("Microsoft. XMLHTTP "); var username = document. getElementById ("txt_username "). value; var age = document. getElementById ("txt_age "). value; var data = "username =" + encodeURIComponent (username) + "& age =" + encodeURIComponent (age); // you do not need to worry about caching xmlHttp. open ("post", "Server. aspx ", true); // required; otherwise, the server cannot receive the xmlHttp parameter. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); xmlHttp. onreadystatechange = function () {if (xmlHttp. readyState = 4 & xmlHttp. status = 200) {document. getElementById ("result "). innerHTML = xmlHttp. responseText ;}} // send the request, which requires data xmlHttp. send (data );} |
Differences:
1. Pay attention to cache issues for get requests. post requests do not need to worry about this issue.
2. For post requests, the Content-Type value must be set to application/x-form-www-urlencoded.
3. When sending a request, because the get request parameters are in the url, the send function sends a null parameter, while the post request needs to assign the parameter to the send method.
For the server. aspx requested in the client code, let's look at the code in server. aspx. cs:
Protected void Page_Load (object sender, EventArgs e) {string username = string. empty; int age = 0; if (Request. httpMethod. toUpper (). equals ("GET") {username = Request. queryString ["username"]; age = int. parse (Request. queryString ["age"]);} else {username = Request. form ["username"]; age = int. parse (Request. form ["age"]);} Response. clear (); Response. write ("Name: '" + username + "' <br/> age:" + age + "<br/> time: '" + DateTime. now. toString () + "'"); Response. end ();}
Here, we find the difference between get requests and post requests on the server:
When the client uses a get Request, the server uses Request. QueryString to obtain parameters. When the client uses a post Request, the server uses Request. Form to obtain parameters.
We can also use a common Request ["username"] method to obtain data from the server. However, this method has a problem.
Next, let's use HttpWatch to see what the client sends and what it receives when requests are sent using get and post methods.
Do not care about the time difference between get requests and post requests, because the get and post buttons are pressed at different times.
|
OverView |
Get request |
|
Post request |
|
The request url shows that the get request carries parameters, while the post request url does not.
|
Header |
Get request |
|
Post request |
|
Because the same server is accessed, the information obtained from the server is consistent, but the client sends different messages.
|
Header |
Get request |
|
Post request |
|
From the cache, we can see that after a get request is sent, It is cached, while when a post request is sent, it is never cached.
|
Query String |
Get request |
|
Post request |
|
Because the get request parameters are in the url, the Query String has a value, while the post request does not.
|
POST Data |
Get request |
|
Post request |
|
In Post Data, because the get request string is included in the url, there is no Data in Post Data.
|
Content (data obtained from the server) |
Get request |
|
Post request |
|
The content obtained from the server is consistent.
|
|
Stream |
Get request |
The |
GET/AjaxWeb/Article7/Server. aspx? Username = % E5 % BC % A0 % E4 % B8 % 89 & age = 33 & random = 0.34838340505348675 HTTP/1.1 Accept :*/* Accept-Language: zh-cn Referer: http: // localhost/AjaxWeb/Article7/Ajax.htm Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 );. net clr 1.1.4322 ;. net clr 2.0.50727 ;. net clr 3.0.04506.648 ;. net clr 3.5.21022; InfoPath.2 ;. NET4.0C ;. NET4.0E) Host: localhost Connection: Keep-Alive |
Obtained from the server |
HTTP/1.1 200 OK Date: Sun, 05 Jun 2011 15:36:27 GMT Server: Microsoft-Microsoft IIS/6.0 X-Powered-By: ASP. NET X-AspNet-Version: 4.0.30319 Cache-Control: private Content-Type: text/html; charset = UTF-8 Content-Length: 60 : 'Zookeeper zookeeper '<br/> zookeeper: 33 <br/> zookeeper: '2017-6-5 23:36:27' |
Post request |
The |
POST/AjaxWeb/Article7/Server. aspx HTTP/1.1 Accept :*/* Accept-Language: zh-cn Referer: http: // localhost/AjaxWeb/Article7/Ajax.htm Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 );. net clr 1.1.4322 ;. net clr 2.0.50727 ;. net clr 3.0.04506.648 ;. net clr 3.5.21022; InfoPath.2 ;. NET4.0C ;. NET4.0E) Host: localhost Content-Length: 34 Connection: Keep-Alive Cache-Control: no-cache Username = % E5 % BC % A0 % E4 % B8 % 89 & age = 33 |
Obtained from the server |
HTTP/1.1 200 OK Date: Sun, 05 Jun 2011 15:47:39 GMT Server: Microsoft-Microsoft IIS/6.0 X-Powered-By: ASP. NET X-AspNet-Version: 4.0.30319 Cache-Control: private Content-Type: text/html; charset = UTF-8 Content-Length: 60 : 'Zookeeper zookeeper '<br/> zookeeper: 33 <br/> zookeeper: '2017-6-5 23:47:39' |
In comparison, the get request url contains parameters, and the post request url does not contain parameters. The post request will not be cached.
Now let's think about another question:
As we mentioned earlier, when the server accepts parameters, it can use a common method, namely: Request ["username"] to accept parameters. This method can accept parameters sent by get and post requests, so let's perform a test, set Content-Type in the get request, and send username = zhangsan in the send method. Let's see what the server returns? Modify the server code as follows:
Protected void Page_Load (object sender, EventArgs e) {string username = string. empty; int age = 0; username = Request ["username"]; age = int. parse (Request ["age"]); Response. clear (); Response. write ("Name: '" + username + "' <br/> age:" + age + "<br/> time: '" + DateTime. now. toString () + "'"); Response. end ();}
In the client, modify btn_get_click () as follows:
// Directly enter Michael Jacob as the username parameter value xmlHttp. open ("get", "Server. aspx? Username = "+ encodeURIComponent (" zhangsan ") +" & age = "+ encodeURIComponent (age) +" & random = "+ Math. random (); // Add the Content-Type information xmlHttp In the get request. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");... // send the request with username = zhangsanxmlHttp. send (username = "zhangsan ");
Test code. The output result is "James ".
Similarly, next to the code above, let's perform another test, modify the post request, and add a username parameter to the url of the open method. The value is zhangsan.
xmlHttp.open("post", "Server.aspx?username=zhangsan", true);
Now let's run the project again. What is the result returned by the server? The result is zhangsan.
When we put parameters in the url and the data of the send method in get and post requests, why is it always the parameter value in the url?
A: When a Request is used, it will traverse from QueryString, Form, and ServerVariable. If the data that meets the requirements is found, the backward search will be stopped. therefore, the username obtained in the above example is actually the username value in the url.
When to use Get requests and Post requests
The purpose of a Get request is to give the server some parameters to obtain the list from the server. For example: list. aspx? Page = 1, indicating to get the data on the first page
The purpose of the Post request is to send some parameters to the server, such as the content in form.
The following example shows the difference between a Get request and a Post request when sending the same data segment.