What is the difference between post and get in ajax request
Difference: 1. get adds the parameter data queue to the URL pointed to by the ACTION attribute of the submitted form, while post uses the “HTTP post” mechanism to place each field in the form and its content in the “HTML HEADER” and transmit it to ACTION together The URL address of the attribute; 2. In 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.
The operating environment of this article: windows10 system, javascript1.8.5&&html5 version, Dell G3 computer.
What is the difference between post and get in ajax request
1. get is to add the parameter data queue to the URL pointed to by the ACTION attribute of the submitted form, and the value corresponds to each field in the form, which can be seen in the URL. Post is through the HTTP post mechanism, each field in the form and its content are placed in the HTML HEADER and sent to the URL address pointed to by the ACTION attribute. The user 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. The parameters of both methods can be obtained with Request.
3. The amount of data transferred by get is small and cannot be larger than 2KB. The amount of data transmitted by post is relatively large, and is generally defaulted to be unlimited. But in theory, it varies from server to server.
4. The security of get is very low, and the security of post is high.
5. With is the same, that is to say, the parameter list on the back of the action page will be ignored; with is not the same.
in addition
A Get request has the following characteristics: it appends data to the URL, which is passed to the server in this way, usually with a question mark? Represents the end of the URL address and the beginning of the data parameter. Each data parameter of the following parameters appears in the form of “name=value”, and a connector & is used to distinguish between parameters.
The Post request has the following characteristics: the data is placed in the HTTP body, and there are more than one organization methods, including the & connection method and the delimiter method, which can hide parameters and transmit large amounts of data, which is more convenient.
All in all: when we submit a form, we usually use post, and when we want to send a larger data file, we need to use post. When the passed value only needs to use the parameter method (the value is not greater than 2KB), the get method can be used.
So the usage of the two for ajax submission is naturally clear.
Expand your knowledge:
So how to choose get and post?
The purpose of the get request is to give the server some parameters to get the list from the server. For example: list.aspx?page=1, which means to get the data of the first page
- If the call is to retrieve data on the server, use get. Another thing to note is that if the value to be retrieved changes with time and the update process, add a random number or timestamp to the get call, so that Subsequent calls will not use the previous incorrect cache. Compared to post, get is simpler and faster, and works in most cases.
The purpose of the post request is to send some parameters to the server
- Unable to use cache file (update file or database on server), use post
- Send a large amount of data to the server (post has no data limit), use post
- post is more stable and reliable than get when sending user input containing unknown characters
We know that the purpose of get is to get information just like its name. It’s designed to display the information on the page that you want to read. The browser buffers the result of a get request, and if the same get request is made again, the browser displays the buffered result instead of rerunning the entire request. This process is different from the browser’s process, but it is intentionally designed to make get calls more efficient. The get call retrieves the data to be displayed on the page, the data will not be changed on the server, so re-requesting the same data will get the same result.
The post method should be used where server information needs to be updated. If a call is to change data stored on the server, the results returned from two identical post calls may be completely different because the value of the second post call is not the same as the value of the first, because the first calls have already updated some of these values. The post call usually gets the response from the server instead of keeping the previous response in the cache.
get request
oBtn.onclick = function() {
var xhr = null;
try {
xhr = new XMLHttpRequest();
} catch (e) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
/*
1.缓存 在url?后面连接一个随机数,时间戳
2.乱码 编码encodeURI
*/
xhr.open('get','2.get.php?username='+encodeURI('刘伟')+'&age=30&' + new Date().getTime(),true);
xhr.send();
xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
alert( xhr.responseText );
} else {
alert('出错了,Err:' + xhr.status);
}
}
}
}
post request
oBtn.onclick = function() {
var xhr = null;
try {
xhr = new XMLHttpRequest();
} catch (e) {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
xhr.open('post','2.post.php',true);
//post方式,数据放在send()里面作为参数传递
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');//申明发送的数据类型
//post没有缓存问题
//无需编码
xhr.send('username=刘伟&age=30');
xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
alert( xhr.responseText );
} else {
alert('出错了,Err:' + xhr.status);
}
}
}
}