Home >Web Front-end >Front-end Q&A >What is the difference between post and get in ajax request?

What is the difference between post and get in ajax request?

WBOY
WBOYOriginal
2022-07-01 17:04:253529browse

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 and its content in the form in the "HTML HEADER" " is sent together to the URL address pointed to by the ACTION attribute; 2. In get mode, the server side uses "Request.QueryString" to obtain the value of the variable. For post mode, the server side uses "Request.Form" to obtain the submitted data.

What is the difference between post and get in ajax request?

The operating environment of this article: windows10 system, javascript1.8.5&&html5 version, Dell G3 computer.

What is the difference between post and get when making an ajax request?

1. Get adds the parameter data queue to the URL pointed to by the ACTION attribute of the submitted form. The value is the same as each field in the form. One correspondence can be seen in the URL. Post uses the HTTP post mechanism to place each field in the form and its content in the HTML HEADER and transmit it to the URL address pointed to by the ACTION attribute. Users cannot see this process.

2. For the get method, the server side uses Request.QueryString to obtain the value of the variable. For the post method, the server side uses Request.Form to obtain the submitted data. Parameters in both ways can be obtained using Request.

3. The amount of data transmitted by get is small and cannot be larger than 2KB. The amount of data transmitted by post is relatively large and is generally unrestricted by default. But in theory, it varies from server to server.

4. The security of get is very low, but the security of post is high.

5. He and are the same, that is to say, the parameter list at the end of the action page will be ignored; but He is different.

In addition

Get request has the following characteristics: it will add data to the URL and pass it to the server in this way, usually using a question mark? Represents the end of the URL address and the beginning of the data parameters. Each data parameter of the following parameters appears in the form of "name=value", and the parameters are distinguished by a connector &.

Post request has the following characteristics: the data is placed in the HTTP body, and it is organized in more than one way, including the & connection method and the delimiter method. It can hide parameters and transfer large amounts of data, which is more convenient.

In short: when we submit a form, we usually use the post method. When we want to transfer a larger data file, we need to use post. When the value passed only needs to be in parameter mode (the value is not larger than 2KB), use the get method.

So the usage of both ajax submissions is naturally clear.

Expand knowledge:

So how to choose get and post?

The purpose of the get request is to give the server some parameters in order to obtain the list from the server. For example: list.aspx?page=1, which means to obtain the data of the first page

  • If the call is to retrieve data on the server, use get. In addition, it should be noted that if the value to be retrieved will change with time and the update process, you must add a random number or Timestamp so that subsequent calls don't use the previous incorrect cache. Compared to post, get is simpler and faster, and works in most situations.

The purpose of the post request is to send some parameters to the server

  • Cache files cannot be used (updating files or databases on the server), use post

  • Send a large amount of data to the server (post has no data limit). When using post

  • to send user input containing unknown characters, post is faster than get is more stable and reliable

We know that the purpose of get is to obtain information just like its name. It is designed to display the information on the page that you want to read. The browser will buffer the execution result of the get request. If the same get request is issued again, the browser will display the buffered result instead of rerunning the entire request. This process is different from the browser's processing, but it is intentionally designed to make get calls more efficient. The get call will retrieve the data to be displayed on the page. The data will not be changed on the server, so you will get the same results when you re-request the same data.

The post method should be used where server information needs to be updated. For example, if a call changes 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 different from the value of the first. calls have updated some of these values. The post call usually fetches the response from the server rather than keeping a cache of the previous response.

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);
}
}
}
}

[Related tutorial recommendations: AJAX video tutorial]

The above is the detailed content of What is the difference between post and get in ajax request?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn