In Ajax, we often use get and post requests. When will get requests and post requests be used? Before answering the questions, we must first understand the difference between get and post.
1. Get adds the parameter data queue to the URL referred to by the Action attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL. Post uses the http post mechanism to place fields in the form and their content in the HTML header and send them to the URL address referred to by the Action attribute. You cannot see this process.
2. For the get method, the server uses request. querystring to obtain the value of the variable. For the POST method, the server uses request. Form to obtain the submitted data. You can use request to obtain parameters in either of the two methods.
3. The data volume transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, which is generally not restricted by default. However, in theory, it varies with the server.
4. Low get security and high post security.
5. <form method = "get" Action = "A. asp? B = B "> with <form method =" get "Action =". ASP "> is the same. That is to say, when the method is get, the parameter list behind the action page is ignored, while <form method =" Post "Action =". asp? B = B "> unlike <form method =" Post "Action =" A. asp ">.
In addition
A GET request has the following features: it adds data to a URL and passes the data to the server in this way. Generally, a question mark is used? It indicates the end of the URL address and the beginning of the Data parameter. Each Data parameter in the following parameter appears in the form of "name = value". A connector & is used to distinguish between the parameter and the parameter.
POST requests have the following features: data is stored in the HTTP body. They are organized in different ways, including connection and delimiter, which can hide parameters and transmit a large amount of data, convenient.
Through the above instructions, we now have a general idea of when to use get and when to use post, right! When submitting a form, we usually use the POST method. When we want to transmit a large data file, we need to use post. When the passed value only needs to be in the parameter mode (this value is not greater than 2 kb), use the get method.
Now let's look at the difference between the get method and the post method when a request is sent through a URL. In the following example, we can easily see the difference between sending the same data through get and post. The sent data is username = Michael JACOB:
In get mode, enter http: // localhost? in the browser? Username = James
Get/? Username = % E5 % BC % A0 % E4 % B8 % 89HTTP/1.1
Accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/vnd. MS-PowerPoint, application/vnd. MS-Excel, application/MSWord ,*/*
Accept-language: ZH-CN
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. Net CLR 1.1.4322)
HOST: localhost
Connection: keep-alive
POST method:
Post, HTTP, 1.1
Accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/vnd. MS-PowerPoint, application/vnd. MS-Excel, application/MSWord ,*/*
Accept-language: ZH-CN
Content-Type: Application/X-WWW-form-urlencoded
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. Net CLR 1.1.4322)
HOST: localhost
Content-Length: 28
Connection: keep-alive
Username = % E5 % BC % A0 % E4 % B8 % 89
The difference is that a form parameter and value are included in a URL request, and a form parameter and value is included in the message entity of an HTTP request.
After comparing the above two paragraphs, we will find that the get method places the Form Content in the front request header, while the POST method places the content in the Request body, meanwhile, set the request Content-Type header to application/X-WWW-form-urlencoded in post. the sent body is the same. You can construct a form to submit the body as follows:
Encodeuricomponent (arg1) = encodeuricomponent (value1) & encodeuricomponent (arg2) = encodeuricomponent (value2 )&.....
Note: encodeuricomponent returns a New String object (in unicode format) containing charstring content. All spaces, punctuation marks, accents, and other non-ASCII characters are replaced by % XX encoding, XX indicates the hexadecimal number of the character. For example, "% 20" is returned by a space ". The characters with a value greater than 255 are stored in % uxxxx format. See the encodeuricomponent () method in JavaScript.
After learning about the above content, we now use the XMLHTTPRequest object of Ajax to send some data to the server in the get and post methods.
Get Method
VaR postcontent = "name =" + encodeuricomponent ("xiaocheng") + "& Email =" + encodeuricomponent ("[email protected]");
XMLHTTP. Open ("get", "somepage" + "? "+ Postcontent, true );
XMLHTTP. Send (null );
POST method
VaR postcontent = "name =" + encodeuricomponent ("xiaocheng") + "& Email =" + encodeuricomponent ("[email protected]");
XMLHTTP. Open ("Post", "somepage", true );
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
// XMLHTTP. setRequestHeader ("Content-Type", "text/XML"); // If an XML file is sent
XMLHTTP. Send (postcontent );
Reprinted from: http://www.javaeye.com/topic/148033
Get and post requests in Ajax