Home >Backend Development >PHP Tutorial >The difference between POST and GET in AJAX and how to implement it

The difference between POST and GET in AJAX and how to implement it

巴扎黑
巴扎黑Original
2016-11-30 10:22:101186browse

We know that the first parameter in the AJAX open method represents the transfer method. According to a lot of information, you can use GET, POST, HEAD and any other transfer method supported by the server to transfer variables. But what are the actual differences between POST and GET, and how should they be implemented in the actual encoding process?

(Note: For the delivery method, please refer to the provisions and instructions of RFC2068)

Everyone knows that in the send method in AJAX, under normal circumstances, null values ​​​​are transferred, but in the post method, in the send() method What is passed are parameters.

For POST, you can use data to save data, but for GET, the parameters must be unique (I think so??)

In the GET process, the parameters are sent together with the URL. http://localhost/ajaxtest/proceajax.php?name=stven&pws=123

But during the POST process, the data information that needs to be transmitted can be taken directly after the ? number.

post_data=name=stven&pws=123

plus loop using js

if (ajax_request_type == "GET") {
if (uri.indexOf("?") == -1)
uri = uri + "?rs=" + func_name;
else
uri = uri + "&rs=" + func_name;
for (i = 0; i < args.length-1; i++)
uri = uri + "&rsargs[] =" + args[i];
uri = uri + "&rsrnd=" + new Date().getTime();
post_data = null;
} else {
post_data = "rs=" + func_name;
for (i = 0; i < args.length-1; i++)
post_data = post_data + "&rsargs[]=" + urlencode(args[i]);
}

function ajax_run(action_name,uri,request_type,postvar) {
var post_data;
ajaxok = ajax_init_object();

if(request_type == "POST"){
uri = uri + "?nocache=" + new Date().getTime();
}else{
uri = uri + "&nocache=" + new Date().getTime();
}

if(ajaxok){
ajaxok.open(request_type, uri, false);

if (request_type == "POST") {
ajax.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}

ajaxok .onreadystatechange = ajax_complete;
ajaxok.send(post_data);
ajaxok = null;
}
}


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