Detailed examples of the differences in usage between JS and Ajax Get and Post
- 2021-06-28 08:25:25
- OfStack
The biggest difference between the get and post methods is that:
1. The get method pass-through parameter is in url, while the post parameter is in send
2. The post method must be added
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
The following example shows the get method
xmlHttp.open("GET","for.php?text="+url,true);
In post it is shown as:
xmlHttp.open("POST","for.php",true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
POST and GET methods share files
index.php
<script src="a.js" type="text/javascript"></script>
<a href="#" onClick="funphp100('o')">o</a>
<a href="#" onClick="funphp100('t')">t</a>
<a href="#" onClick="funphp100('x')">x</a>
<div id="php100"></div>
POST method file:
a.js
var xmlHttp;
function S_xmlhttprequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function funphp100(n){
var data = "text=" +n; * // Multiple parameters, followed by
S_xmlhttprequest();
xmlHttp.open("POST","for.php",true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange=byphp;
xmlHttp.send(data);
}
function byphp(){
var byphp100=xmlHttp.responseText;
document.getElementById("php100").innerHTML=byphp100;
}
for.php:
<?
echo $_POST['text'];
?>
GET method file:
a.js:
var xmlHttp;
function S_xmlhttprequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}
function funphp100(url){
S_xmlhttprequest();
xmlHttp.open("GET","for.php?text="+url,true);
xmlHttp.onreadystatechange=byphp;
xmlHttp.send(null);
}
function byphp(){
var byphp100=xmlHttp.responseText;
document.getElementById("php100").innerHTML=byphp100;
}
for.php:
<?
echo $_GET['text'];
?>