什么時(shí)候用get請(qǐng)求,什么時(shí)候用post方式請(qǐng)求呢? 在做回答前我們首先要了解get和post的區(qū)別.
1、get是把參數(shù)數(shù)據(jù)隊(duì)列加到提交表單的ACTION屬性所指的URL中,值和表單內(nèi)各個(gè)字段一一對(duì)應(yīng),在URL中可以看到。post是通過HTTPpost機(jī)制,將表單內(nèi)各個(gè)字段與其內(nèi)容放置在HTML HEADER內(nèi)一起傳送到ACTION屬性所指的URL地址。用戶看不到這個(gè)過程。
2、 對(duì)于get方式,服務(wù)器端用Request.QueryString獲取變量的值,對(duì)于post方式,服務(wù)器端用Request.Form獲取提交的數(shù)據(jù)。兩種方式的參數(shù)都可以用Request來獲得。
3、get傳送的數(shù)據(jù)量較小,不能大于256KB。post傳送的數(shù)據(jù)量最大為4M,一般被默認(rèn)為不受限制。但理論上,因服務(wù)器的不同而異.
4、get安全性非常低,post安全性較高。
5、 <form method="get" action="a.asp?b=b">跟<formmethod="get"action="a.asp">是一樣的,也就是說,method為get時(shí)action頁面后邊帶的參數(shù)列表會(huì)被忽視;而<formmethod="post" action="a.asp?b=b">跟<form method="post"action="a.asp">是不一樣的。
另外
Get請(qǐng)求有如下特性:它會(huì)將數(shù)據(jù)添加到URL中,通過這種方式傳遞到服務(wù)器,通常利用一個(gè)問號(hào)?代表URL地址的結(jié)尾與數(shù)據(jù)參數(shù)的開端,后面的參數(shù)每一個(gè)數(shù)據(jù)參數(shù)以“名稱=值”的形式出現(xiàn),參數(shù)與參數(shù)之間利用一個(gè)連接符&來區(qū)分。
Post請(qǐng)求有如下特性:數(shù)據(jù)是放在HTTP主體中的,其組織方式不只一種,有&連接方式,也有分割符方式,可隱藏參數(shù),傳遞大批數(shù)據(jù),比較方便。
通過以上的說明,現(xiàn)在我們大致了解了什么時(shí)候用get什么時(shí)候用post方式了吧,對(duì)!當(dāng)我們?cè)谔峤槐韱蔚臅r(shí)候我們通常用post方式,當(dāng)我們要傳送一個(gè)較大的數(shù)據(jù)文件時(shí),需要用post。當(dāng)傳遞的值只需用參數(shù)方式(這個(gè)值不大于256KB)的時(shí)候,用get方式即可。
現(xiàn)在我們?cè)倏纯赐ㄟ^URL發(fā)送請(qǐng)求時(shí),get方式和post方式的區(qū)別。用下面的例子可以很容易的看到同樣的數(shù)據(jù)通過GET和POST來發(fā)送的區(qū)別, 發(fā)送的數(shù)據(jù)是 username=張三 :
GET 方式, 瀏覽器鍵入 http://localhost?username=張三
GET /?username=%E5%BC%A0%E4%B8%89 HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,application/vnd.ms-powerpoint, application/vnd.ms-excel,application/msword, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
Host: localhost
Connection: Keep-Alive
POST 方式:
POST / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,application/vnd.ms-powerpoint, application/vnd.ms-excel,application/msword, */*
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
Host: localhost
Content-Length: 28
Connection: Keep-Alive
username=%E5%BC%A0%E4%B8%89
區(qū)別就是一個(gè)在 URL 請(qǐng)求里面附帶了表單參數(shù)和值, 一個(gè)是在 HTTP 請(qǐng)求的消息實(shí)體中。
比較一下上面的兩段文字, 我們會(huì)發(fā)現(xiàn) GET 方式把表單內(nèi)容放在前面的請(qǐng)求頭中, 而 POST 則把這些內(nèi)容放在請(qǐng)求的主體中了,同時(shí) POST 中把請(qǐng)求的 Content-Type 頭設(shè)置為 application/x-www-form-urlencoded.而發(fā)送的正文都是一樣的, 可以這樣來構(gòu)造一個(gè)表單提交正文:
encodeURIComponent(arg1)=encodeURIComponent(value1)&encodeURIComponent(arg2)=encodeURIComponent(value2)&.....
注: encodeURIComponent 返回一個(gè)包含了 charstring 內(nèi)容的新的 String 對(duì)象(Unicode格式), 所有空格、標(biāo)點(diǎn)、重音符號(hào)以及其他非 ASCII 字符都用 %xx 編碼代替,其中 xx 等于表示該字符的十六進(jìn)制數(shù)。例如,空格返回的是 "%20" 。 字符的值大于 255 的用 %uxxxx 格式存儲(chǔ)。參見 JavaScript 的encodeURIComponent() 方法.
在了解了上面的內(nèi)容后我們現(xiàn)在用ajax的XMLHttpRequest對(duì)象向服務(wù)器分別用GET和POST方式發(fā)送一些數(shù)據(jù)。
GET 方式
var postContent ="name=" + encodeURIComponent("xiaocheng") + "&email=" + encodeURIComponent("xiaochengf_21@yahoo.com.cn");
xmlhttp.open("GET", "somepage" + "?" + postContent, true);
xmlhttp.send(null);
POST 方式
var postContent ="name=" + encodeURIComponent("xiaocheng") + "&email=" + encodeURIComponent("xiaochengf_21@yahoo.com.cn");
xmlhttp.open("POST", "somepage", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//xmlhttp.setRequestHeader("Content-Type", "text/xml"); //如果發(fā)送的是一個(gè)xml文件
xmlhttp.send(postContent);
聯(lián)系客服