|
在HTML中,form元素用method屬性來指定有兩種不同的提交方法,即"get"(默認值)和"post"。 1. get和post的定義 使用get時,form的數(shù)據(jù)集(形如control-name=current-value的鍵值對)被附加到form元素的action屬性所指定的URI后面; <form method="get" action="FormGet.aspx"> <input type="text" name="ProductID" value="1" /> <input type="submit" value="Get" /> </form>
這個form在提交的時候,會產(chǎn)生這樣能夠一個get請求:FormGet.aspx?ProductID=1。 而post會把form的數(shù)據(jù)集,即ProductID=1這個鍵值對包裝在請求的body中,發(fā)送給服務(wù)器,然后向服務(wù)器請求數(shù)據(jù)。對于: <form method="post" action="FormPost.aspx"> <input type="text" name="ProductID" value="1" /> <input type="submit" value="Get" /> </form>
這樣一個form在提交時,我們將看到一個干凈的URI:FormPost.aspx。因為數(shù)據(jù)不是拼接在URI中。 2. get和post的區(qū)別 2.2 編碼 注意get和post對應(yīng)的enctype屬性有區(qū)別。enctype有兩個值,默認值為application/x-www-form-urlencoded,而另一個值multipart/form-data只能用于post。 2.3 提交的數(shù)據(jù)的長度 而對于post,因為提交的數(shù)據(jù)不在url中,所以通??梢院唵蔚卣J為數(shù)據(jù)長度限制僅受限于服務(wù)器的設(shè)置。 2.4 緩存 2.5 引用和SEO 3. 服務(wù)端的處理 我們也可以籠統(tǒng)地使用Request[control-name]。但這樣做的效率不如前者。我們可以用下面的程序比較Request.QueryString和Request的效率: <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <mce:script runat="server"><!-- protected void Page_PreInit(object sender, EventArgs e) { if(Request["InputString"] != null) { int count = 1000000; DateTime start; DateTime end; string value = ""; start = DateTime.Now; for(int i = 0;i < count;i++) { value = Request.QueryString["InputString"]; } end = DateTime.Now; double requestGet = (end - start).TotalSeconds; start = DateTime.Now; for(int i = 0;i < count;i++) { value = Request["InputString"]; } end = DateTime.Now; double request = (end - start).TotalSeconds; compare.InnerHtml = requestGet.ToString() + " / " + request.ToString() + " = " + (requestGet / request).ToString(); get.InnerHtml = value; } } // --></mce:script> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Request.QueryString / Request</title> </head> <body> <form method="get" action="FormGet.aspx"> <div> <input type="text" name="InputString" /><input type="submit" value="Post" /><br /> Get: <span runat="server" id="get"></span><br /> Request.QueryString / Request: <span runat="server" id="compare"></span> </div> </form> </body> </html>
同樣的辦法我們可以比較Request.Form和Request。 最后得到的結(jié)果(Request.QueryString[control-name] / Request[control-name]和Request.Form[control-name] / Request[control-name])大多數(shù)時候是小于1的。因此,我們因該盡量用Request.QueryString或 Request.Form來代替Request。 4. 正確地使用get和post 打個比方,如果你提交一個form會從Google上查詢一個關(guān)鍵詞,那么我們可以認為這個form是冪等的,因為1次提交和10次提交的副作用是差不多的(10次查詢可能會多消耗一些電能);如果你提交一個form是訂購一個終極大黃蜂(Utimate bumblebee),那么這就不是冪等的:要是你不小心多提交了1次form的話,你可能會被老婆亂罵,你不小心又提交了10次的話,你可能就破產(chǎn)了——一次提交和多次提交的副作用明顯不同,所以這不是冪等的。 所以,一般來說,如果提交這個請求純粹只是從服務(wù)端獲取數(shù)據(jù)而不進行其他操作,并且多次提交不會有明顯的副作用,應(yīng)該使用get。比如: 搜索引擎的查詢:http://www.google.com/search?q=yandixin; 修改服務(wù)器上數(shù)據(jù)庫中的數(shù)據(jù); 5. 瀏覽器差異
Opera 9.24:正常(自動post數(shù)據(jù)); 6. 參考
|
|
|
來自: 集微筆記 > 《Web開發(fā)》