1. 程式人生 > >原生Get請求和Post請求

原生Get請求和Post請求

esp ext for read type 理論 stat 應用 str

get()請求

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("get","url",true);
xmlhttp.send();

}

post()請求

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","/ajax/demo_post2.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Bill&lname=Gates");
}

兩者之間的區別

get()

1.存放位置:請求放入請求行中 url後

2.長度限制:2Kb

3.效率:相對高

4.只應用於取回數據

post()

1.存放位置:請求放入請求體中

2.長度限制:理論上沒有長度限制

3.效率:相對低

4.安全性:相對於get()高一些 但兩者都沒有絕對的安全

原生Get請求和Post請求