Introduction to get and post usage differences in Ajax

Source: Internet
Author: User
Tags html header http post


1, get is to add the parameter data queue to submit the form of the action attribute refers to the URL, the value and the form of each field one by one corresponding, in the URL can be seen. Post is the HTTP post mechanism that places the fields in the form and their contents in the HTML header to the URL address that the action attribute refers to. This process is not visible to the user.



2, for Get way, server end uses Request.QueryString to obtain variable value, for post way, server end uses Request.Form to obtain the submitted data. The parameters of both methods can be obtained by request.



3. The amount of data transferred by get is less than 2KB. Post transfers have a large amount of data, which is generally default to unrestricted. In theory, however, it varies depending on the server.



4, get security is very low, post security is high.



5, <form method= "get" action= "A.asp?b=b" > with <form method= "get" action= "a.asp" > is the same, that is to say, The list of arguments behind the action page when method is get is ignored, while <form method= "post" action= "A.asp?b=b" > and <form method= "Post action=" A.asp "> is not the same.



Other than that



A GET request has the following characteristics: It adds data to the URL, which is passed to the server in this way, usually using a question mark? Represents the end of a URL address and the beginning of a data parameter, with each data parameter in the form of a "name = value", which is differentiated between parameters and parameters using a connector &.
Post requests have the following characteristics: The data is placed in the HTTP body, the organization of more than one, there are & connection, but also have the way of dividing, can hide parameters, transfer a large number of data, more convenient.



Through the above instructions, now we have a general understanding of when to use get when the post way, yes! When we submit the form we usually use post, when we want to send a large data file, we need to use post. When the value passed is only in parameter mode (this value is not more than 2KB), you can use Get method.



Now let's look at the difference between get and post when sending a request through a URL. The following example makes it easy to see the difference between the same data sent via Get and post, and the data sent is Username= John:
The fundamental difference between get and post is as follows:



Get pass the parameter by URL (take the dynamic address http://www.oncoding.cn/?p=480 in this article as an example) and send the request header to obtain the data from the server:


The code is as follows

Get/?p=480 http/1.1

Host:www.oncoding.cn

mozilla/5.0

....

The post also requires a URL and a request header, which requires additional data to be sent to the server and then the server responds with the following data format:

post/wp-login.php http/1.1

Host:www.oncoding.cn

user-agent:mozilla/5.0

....

Userid=admin&password=asdfg


Why do get and post differ in speed?



Post has an extra step to send data than get, and we can understand this process by MIDP implementing the Get and Post program code:


The code is as follows

/MIDP to implement get process (variable definition omitted):
conn = (httpconnection) connector.open (URL);//Establish Connection
Conn.setrequestproperty ("User-agent", Agent); Set the request header
 
int rc = Conn.getresponsecode ();//Get response
//...
//MIDP implementation Post process (Encodeddata for post data):
conn = (httpconnection) connector.open (URL);//Establish Connection
Conn.setrequest Method (Httpconnection.post); Set the request header
Conn.setrequestproperty ("User-agent", Agent);
Conn.setrequestproperty ("Content-type", Type);
Conn.setrequestproperty ("content-length", 
                 encodeddata.length ());
 
OutputStream os = Conn.openoutputstream ();//Send Data
Os.write (Encodeddata.getbytes ());
 
Int rc = Conn.getresponsecode ();//Get response



The difference is that a form parameter and value are included in the URL request, one in the message entity of the HTTP request.



By comparing the two paragraphs above, we will find that the Get mode places the form content in the previous request header, while Post puts the content in the requested body, while the Content-type header of the request is set to Application/x-www-form-ur in the post. Lencoded. The text that is sent is the same, so you can construct a form submission body:


The code is as follows
encodeURIComponent (arg1) =encodeuricomponent (value1) &encodeuricomponent (arg2) =encodeuricomponent (value2) The ...


Note: encodeURIComponent returns a new String object (Unicode format) that contains charstring content, and all spaces, punctuation, accented symbols, and other non-ASCII characters are replaced with%xx encodings, where xx equals the The hexadecimal number. For example, a space returns "%20". Characters with a value greater than 255 are stored in%UXXXX format. See the encodeURIComponent () method for JavaScript.



Having learned the above, we are now using AJAX XMLHttpRequest objects to send some data to the server separately using get and post methods.



Get mode


The code is as follows Copy Code
var postcontent = "Name=" + encodeuricomponent ("Xiaocheng") + "&email=" + encodeURIComponent ("xiaochengf_21@ Yahoo.com.cn ");
Xmlhttp.open ("Get", "somepage" + "?" + Postcontent, True);
Xmlhttp.send (NULL);



POST method


The code is as follows

var postcontent = "Name=" + encodeuricomponent ("Xiaocheng") + "&email=" + encodeURIComponent ("xiaochengf_21@ Yahoo.com.cn ");
Xmlhttp.open ("POST", "Somepage", true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.setrequestheader ("Content-type", "Text/xml"); If you are sending an XML file
Xmlhttp.send (postcontent);


What is the difference in speed between get and post for Ajax?



Just have a foreigner wrote a Get and post Requests in Ajax, compare get and post in Ajax speed problem, write a voluminous, but there is no data and theory, the browser test results are only "Very slow" and "Fast" ...



We simply write our own program to test the difference in speed by sending Ajax requests before and after receiving AJAX data. Comparison is made using pure JavaScript and jquery in two different ways.



Test Demo Here | Download Test Package (please change the request interval according to the speed, otherwise it will cause confusion)



Several browsers on hand tested, PHP program on the U.S. DreamHost server, through the Shandong Telecom Network test results are as follows:



Firefox 3.5









Chrome 4.0.266 Beta









IE8









IE6 in the virtual machine:









Firefox 2.0 in a virtual machine:












And now you're browsing jquery, looking at jquery get,post.



$.get () and $.post () usage for Ajax in jquery:
the commonly used load () method can often be used to get static data files from a Web server, which does not reflect the full characteristics of Ajax. In actual development, if you need to pass some parameters to the page in the server, you can use either the $.get () or the $.post () method (or the $.ajax method to be explained later).
Note: the $.get () and $.post () methods are global functions in jquery. The load () method described earlier is an operation on a jquery object. The
$.get () method:
The $.get () method uses a Get method to make an asynchronous request.
Its syntax structure is:
$.get (URL [, data] [, callback] [, type])
$.get () method parameters are interpreted as follows:
parameter name: URL type: String description: The U of the requested HTML page RL address
Parameter name: data (optional) Type: Object Description: Key/value data sent to the server is appended as QueryString to the request URL
Parameter name: callback (optional) Type: Function Description: The callback function when it is loaded (only when the method is called by success when the response returns) automatically passes the request result and state to the method
Parameter name: type (optional) Type: String Description: Server-side return content format, including XML, HTML, Script, JSON, text, and _default


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.