一.HTTP (無狀態的協議)。
1.HTTP請求過程。
(1)建立TCP連接。
(2)web瀏覽器向web服務器發送請求命令。
(3)web瀏覽器發送請求頭信息。
(4)web服務器應答。
(5)web服務器發送應答頭信息。
(6)web服務器向瀏覽器發送數據。
(7)web服務器關閉TCP連接。
一個HTTP請求一般有四部分組成:
(1)HTTP請求的方法或動作,比如get還是post請求。
(2)正在請求的URL。總的知道請求的地址是什麼。
(3)請求頭。包含一些客戶端環境信息,身份驗證信息等。
(4)請求體。也就是請求正文。包含客戶提交的查詢字符串信息,表單信息等。
一般請求頭與請求體會有空行。代表請求頭結束,接下來的是請求體。
GET:一般用於信息獲取,默認請求方法。一般是安全的,主要用於查詢。發送的信息對任何人都是可見的,所有的變量名和值都顯示在URL裏。也就是用URL 傳遞參數。所以 get請求發送的數量也有限制。大約2000 個字符。(冪等:查詢一次 == 一萬次 )
POST:一般用於修改服務器的上的資源。一般存表單,發數據。 都會嵌入HTTP請求體中。也沒有數量的限制。
GET && Post 區別 :
1.GET 訪問瀏覽器 認爲 是冪等的.就是 一個相同的URL只有一個結果(相同是指:整個URL 字符串完全匹配),所以第二次訪問的時候,如果URL字符串沒有變化,瀏覽器是直接拿出第一次的結果的. 所以 查詢一次 == 一萬次 .
POST 則認爲是一個變動性訪問.(瀏覽器認爲POST的提交必定是有改變的.)
爲了防止GET的等冪訪問, 就在 URL 後面加上 ?+new Date(); 因爲,data 每次都會更新,到時url 的字符串 有改變,這樣就能保證,url 不會取緩存裏面的東西了. 當然也可以 添加參數 .
eg: url+"?id="+1+"&name="+"hello" + "&time="+ new Date().getTime();
解釋:
?: 問號代表URL地址的結尾與數據參數的開端
後面的參數每一個數據參數以“名稱=值”的形式出現.參數與參數之間利用一個連接符&來區分
+ : 必須用+ 連接數據 / 變量.
使用get方式需要注意:
1 對於get請求(或凡涉及到url傳遞參數的),被傳遞的參數都要先經encodeURIComponent方法處理.例:var url = "update.php?username=" +encodeURIComponent(username)
+ "&content=" +encodeURIComponent
(content)+"&id=1" ;
使用Post方式需注意:
1.設置header的Context-Type爲application/x-www-form-urlencode確保服務器知道實體中有參數變量. 通常使用XmlHttpRequest對象的SetRequestHeader("Context-Type","application/x-www- form-urlencoded;")。例:
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
2.參數是名/值一一對應的鍵值對,每對值用&號隔開.如 var name=abc&sex=man&age=18,注意var name=update.php?
abc&sex=man&age=18以及var name=?abc&sex=man&age=18的寫法都是錯誤的;
3.參數在Send(參數)方法中發送,例: xmlHttp.send(name); 如果是get方式,直接 xmlHttp.send(null);
4.服務器端請求參數區分Get與Post。如果是get方式則$username = $_GET["username"]; 如果是post方式,則$username = $_POST["username"];
AJAX亂碼問題
產生亂碼的原因:
1、xtmlhttp 返回的數據默認的字符編碼是utf-8,如果客戶端頁面是gb2312或者其它編碼數據就會產生亂碼
2、post方法提交數據默認的字符編碼是utf-8,如果服務器端是gb2312或其他編碼數據就會產生亂碼
解決辦法有:
1、若客戶端是gb2312編碼,則在服務器指定輸出流編碼
2、服務器端和客戶端都使用utf-8編碼
gb2312:header('Content-Type:text/html;charset=GB2312');
utf8:header('Content-Type:text/html;charset=utf-8');
下面屬於轉載部分::
Get 與 Post 的區別
Ajax中我們經常用到get和post請求.那麼什麼時候用get請求,什麼時候用post方式請求呢? 在做回答前我們首先要了解get和post的區別。
- get是把參數數據隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個字段一一對應,在URL中可以看到。post是通過HTTP post機制,將表單內各個字段與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。用戶看不到這個過程。
- 對於get方式,服務器端用Request.QueryString獲取變量的值,對於post方式,服務器端用Request.Form獲取提交的數據。兩種方式的參數都可以用Request來獲得。
- get傳送的數據量較小,不能大於2KB。post傳送的數據量較大,一般被默認爲不受限制。但理論上,因服務器的不同而異。
- get安全性非常低,post安全性較高。
- <form method="get" action="a.asp?b=b">跟<form method="get" action="a.asp">是一樣的,也就是說,method爲get時action頁面後邊帶的參數列表會被忽視;而<form method="post" action="a.asp?b=b">跟<form method="post" action="a.asp">是不一樣的。
當我們在提交表單的時候我們通常用post方式,當我們要傳送一個較大的數據文件時,需要用post。當傳遞的值只需用參數方式(這個值不大於2KB)的時候,用get方式即可。
現在我們再看看通過URL發送請求時,get方式和post方式的區別。用下面的例子可以很容易的看到同樣的數據通過GET和POST來發送的區別,發送的數據是 username=張三。
GET 方式,瀏覽器鍵入 http://localhost?username=張三
1 |
GET /?username=%E5%BC%A0%E4%B8%89 HTTP/1.1 |
2 |
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */* |
4 |
Accept-Encoding: gzip, deflate |
5 |
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322) |
POST 方式:
02 |
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */* |
03 |
Accept-Language: zh-cn |
04 |
Content-Type: application/x-www-form-urlencoded |
05 |
Accept-Encoding: gzip, deflate |
06 |
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322) |
09 |
Connection: Keep-Alive |
10 |
username=%E5%BC%A0%E4%B8%89 |
區別就是一個在 URL 請求裏面附帶了表單參數和值, 一個是在 HTTP 請求的消息實體中。
比較一下上面的兩段文字, 我們會發現 GET 方式把表單內容放在前面的請求頭中, 而 POST 則把這些內容放在請求的主體中了, 同時 POST 中把請求的 Content-Type 頭設置爲 application/x-www-form-urlencoded. 而發送的正文都是一樣的, 可以這樣來構造一個表單提交正文: encodeURIComponent(arg1)=encodeURIComponent(value1)&encodeURIComponent(arg2)=encodeURIComponent(value2)&.....
注: encodeURIComponent 返回一個包含了 charstring 內容的新的 String 對象(Unicode 格式), 所有空格、標點、重音符號以及其他非 ASCII 字符都用 %xx 編碼代替,其中 xx 等於表示該字符的十六進制數。 例如,空格返回的是 "%20" 。 字符的值大於 255 的用 %uxxxx 格式存儲。參見 JavaScript 的 encodeURIComponent() 方法.
在瞭解了上面的內容後我們現在用ajax的XMLHttpRequest對象向服務器分別用GET和POST方式發送一些數據。
GET 方式
1 |
var postContent = "name=" + encodeURIComponent( "xiaocheng" )
+ "&email=" |
3 |
xmlhttp.open( "GET" , "somepage" +
"?" + postContent, true ); |
POST 方式
1 |
var postContent = "name=" + encodeURIComponent( "xiaocheng" )
+ "&email=" |
3 |
xmlhttp.open( "POST" , "somepage" , true ); |
4 |
xmlhttp.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ); |
6 |
xmlhttp.send(postContent); |
get方式
get方式是最爲常見的,一般實現用戶登錄,修改密碼用的都是get方式。
新建一html文檔,body標籤內容如下:
1 |
< body style = "text-align: center" > |
2 |
< input type = "text" id = "txt" /> |
4 |
< input type = "button" value = "get方式回調" onclick = "get()" /> |
js代碼文件
04 |
var str=document.getElementById ( "txt" ).value; |
05 |
var url= "PageAjax.aspx?argument=" +escape(str); |
06 |
xhr.open( "get" ,url, true ); |
07 |
xhr.onreadystatechange=renew; |
16 |
var response=xhr.responsetext; |
17 |
var res=response.split( '\n' ); |
服務器端PageAjax.aspx.cs文件代碼如下
1 |
protected void Page_Load(object sender, EventArgs e) |
3 |
if (Request[ "argument" ] != null ) |
5 |
string res = "成功實現post方式回調!傳入的參數是:" + Request[ "argument" ].ToString()+ "\n" ; |
到此一個簡單的get方式回調完成。
post方式
由於get方式每次都要傳入參數到url地址上,像用戶名,密碼之類的參數由於字符比較少,完全可以考慮這中傳遞方式,但是當有很多參數、並且參數的字符串值很長時(比如博客,你不可能把整篇博客的內容都以參數的方式傳遞到url上),這種方式就不好了,由於有了post方式的出現。
新建一html文檔,body標籤內容如下:
1 |
< textarea id = "TextArea1" style = "width:
323px; height: 76px" ></ textarea > |
3 |
< input id = "Button1" type = "button" value = "post方式回調" onclick = "post()" /> |
js代碼文件
04 |
var str=document.getElementById ( "TextArea1" ).value; |
05 |
var poststr= "arg=" +str; |
06 |
var url= "PageAjax.aspx?time=" + new Date(); |
07 |
xhr.open( "post" ,url, true ); |
08 |
xhr.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ); |
10 |
xhr.onreadystatechange=update; |
19 |
var response=xhr.responsetext; |
20 |
var res=response.split( '\n' ); |
服務器端PageAjax.aspx.cs文件代碼如下
1 |
protected void Page_Load(object sender, EventArgs e) |
3 |
if (Request[ "arg" ] != null ) |
5 |
string res = "成功實現get方式回調!傳入的參數是:" + Request[ "arg" ].ToString()
+ "\n" ; |
到此一個簡單的post方式回調完成。
Get 與 Post 的代碼編寫上的區別
02 |
function createQueryString(){ |
03 |
var firstName = document.getElementById( "firstName" ).value; |
04 |
var secName = document.getElementById( "secName" ).value; |
06 |
var querystring= "firstName=" +firstName+ "&secName=" +secName; |
11 |
function doRequestUsingGET(){ |
12 |
createXMLHttpRequest(); |
13 |
var queryString = "test.php?" ; |
14 |
queryString= queryString + createQueryString()+ "&timstamp=" + new Date().getTime(); |
15 |
Ajax.onreadystatechange=handleStateChange; |
16 |
Ajax.open( "GET" ,queryString, true ); |
22 |
function doRequestUsingPOST(){ |
23 |
createXMLHttpRequest(); |
26 |
var url= "test.php?timstamp=" + new Date().getTime(); |
28 |
var queryString=createQueryString(); |
29 |
Ajax.open( "POST" ,url, true ); |
30 |
Ajax.onreadystatechange=handleStateChange; |
32 |
Ajax.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ) |
34 |
Ajax.send(queryString); |
38 |
function handleStateChange(){ |
39 |
if (Ajax.readyState==4){ |
40 |
if (Ajax.status==200){ .......} |