In the AJAX when to when to use GET requests need to use POST request?

In the AJAX when to when to use GET requests need to use POST request?

When we have a lot of data to be transmitted best way is to pass only a small amount of information to make multiple Ajax calls when. If you are using an Ajax call to send large amounts of data, it is best to end this practice, because it does not save time.

Therefore, the need to transmit large amounts of data can be a reason for us to hesitate between GET and POST it? Both methods are designed for different purposes, the two different purposes are that it uses. This statement applies not only to use GET and POST, but also applicable to other methods.

GET purposes as if its name is used to obtain information. It aims to show the information on the page you want to read. The browser will cushion the results of a GET request, if the same GET request is made again, the browser will display the results of a buffer instead of re-running the entire request. This process is different from the process of the browser, but it is deliberately designed in such a way to make GET calls more efficient. GET call retrieves the data to be displayed in the page, the data can not be changed on the server, so re-request the same data when it will get the same results.

GET request

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;

//添加参数,以求每次访问不同的url,以避免缓存问题
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;
    }
}

//发送请求,参数为null
xmlHttp.send(null);

}

POST method should be used where you need to update the server information. If a call to change the data stored on the server, and the results from two identical POST call returns might be completely different, because the value of the second POST call with the first value is not the same, this is due to the first a call has been updated with some value. POST calls will usually get a response rather than remaining before a response from the server buffer.

POST request

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

//不用担心缓存问题
xmlHttp.open("post", "Server.aspx", true);

//必须设置,否则服务器端收不到参数
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xmlHttp.onreadystatechange = function () {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        document.getElementById("result").innerHTML = xmlHttp.responseText;
    }
}

//发送请求,要data数据
xmlHttp.send(data);

}

Therefore, do not use the amount of data to decide whether to choose the GET or POST, but should make a choice between the two in accordance with the purpose. If the call is to retrieve data from the server using GET. If you want to retrieve the value will change over time and changes in the process of updating the current time will have to add a parameter in the GET call, so it will not call back using the previous incorrect buffer. If the call is to send arbitrary data to a server, you can use POST.

Guess you like

Origin blog.csdn.net/weixin_43776522/article/details/85085641