版權聲明: 本文由 一隻博客 發表於 bloghome博客javascript
HTTP中,提交數據的方式,最經常使用的就是GET和POST。java
GET方式,搞WEB開發的都很熟悉,其實就是把參數編程鍵值對經過QueryString的方式放在URL尾部,好比: git
http://xxxx.com/test.aspx?a=1&b=2ajax
POST方法,一般就是把要提交的表單放在一個FORM中,指明action後就能夠提交數據。沒有發現有諸如?a=1&b=2這樣的鍵值對出現。chrome
其實你們都被屏蔽了。事實上,打開W3C官網,能夠看到以下編程
When the user submits a form (e.g., by activating a submit button), the user agent processes it as follows.json
Step one: Identify the successful controls 瀏覽器
Step two: Build a form data set 服務器
A form data set is a sequence of control-name/current-value pairs constructed from successful controls
Step three: Encode the form data set
The form data set is then encoded according to the content type specified by the enctype attribute of the FORM element.
Step four: Submit the encoded form data set
清楚的寫到,第三步,對數據編碼。根據 content type的內容指定的進行編碼。
If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type. The user agent then traverses the link to this URI. In this scenario, form data are restricted to ASCII codes.
If the method is "post" and the action is an HTTP URI, the user agent conducts an HTTP "post" transaction using the value of the action attribute and a message created according to the content type specified by the enctype attribute.
若是方法是get的,就用問號,編碼方式按照"application/x-www-form-urlencoded" 進行。user agent實際上指的就是瀏覽器,它會處理這一切,並指向一個編碼後生成的Link。
若是是post方式,則會看使用哪一種content type來編碼。經過屬性enctype指定編碼content type的。
POST支持的content type方式有2種,默認的content type是application/x-www-form-urlencoded 。這種編碼方式,其實也就是把表單名字和值組成鍵值對的形式,用‘&’符號鏈接該轉碼的轉碼拼接成個get方式差很少的那種格式。如原文寫道:
application/x-www-form-urlencoded
This is the default content type. Forms submitted with this content type must be encoded as follows:
Control names and values are escaped. Space characters are replaced by `+', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').
The control names/values are listed in the order they appear in the document. The name is separated from the value by `=' and name/value pairs are separated from each other by `&'.
固然HTML4.0後還有multipart/form-data格式的,用來POST文件等二進制數據的。有興趣的能夠本身研究一下。
POST方式其實也是轉成這種鍵值對的形式來post的,那麼咱們能夠本身編寫鍵值對,來post數據了吧?固然能夠。其實Jquery中就是這麼用的。先看個例子。直接使用原生的XMLHttpRequest來做POST操做。
<script type="text/javascript"> var objHTTP, strResult; objHTTP = new XMLHttpRequest(); objHTTP.open('POST', "Handler1.ashx", false); objHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); objHTTP.send("data1=Hello&data2=Wrold"); strResult = objHTTP.responseText; alert(strResult); </script>
服務器端寫(asp.net ashx文件)
public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/plain"; string s = context.Request["data1"] +" "+ context.Request["data2"]; context.Response.Write(s); } </script>
獲得結果以下:
經過chrome也能夠看到:
和經過提交表單的方式是同樣的效果。
再看一下Jquery的ajax方法。
其中data能夠直接被賦值爲key1=value1&key2=value2,若是有人喜歡json格式的賦值方式,那麼ajax方法參數中有一個屬性processData,默認是true,它會把json格式的數據轉換成key1=value1&key2=value2的格式。真是省心省力。以下,兩種賦值方式均可以寫,我的偏心json方式,簡單明瞭。
var option = { url: 'Handler1.ashx', type: 'POST', dataType: 'html', success: function (result) { alert(result); }, //data: { data1: "Hello", data2: "World" }兩種寫法皆能夠 data: "data1=Hello&data2=World" }; $.ajax(option);
注意,這種方式,默認的'Content-Type'都是'application/x-www-form-urlencoded'。若是更換Content-Type,就須要把processData設成false,同時,data的內容要本身本身控制好。
要用ajax方式Post文件就比較困難,由於Content-Type爲multipart/form-data,而data沒法手動賦值,所以比較困難。用firebug,查看post的一個doc文件,發現post的data全是亂碼。
所以ajax方式上傳文件通常都是包含一個<frame>或者使用flash的方式上傳文件。
若是指明Content-Type是'application/json',那麼data須要的是JSON的string,使用方法JSON.stringify(jsondata)能夠得到這種string。可是要注意一點,不使用默認的application/x-www-form-urlencoded,數據是不會在Form Data中的,也就是用傳統Request["data"]方法是得到不到數據的。對於特殊格式的數據就要特殊辦法處理。那處理的方式就是強行讀取Request.InputStream。固然對於默認的application/x-www-form-urlencoded也是能夠採用這種最原始可是最有效的。
好比下面的例子:js部分:
var data = { "data": "testdata" }; var option = { url: 'Handler1.ashx', type: 'POST', data: JSON.stringify(data),//取得json的string dataType: 'html', contentType: 'application/json', success: function (result) { alert(result); } }; $.ajax(option);
傳送的是application/json類型。那麼後臺就得以下寫法:
public void Proce***equest(HttpContext context) { context.Response.ContentType = "text/plain"; StreamReader sr = new StreamReader(context.Request.InputStream, Encoding.UTF8); string s=sr.ReadToEnd(); context.Response.Write(s); }
這裏的s就是純的json字符串,要轉變成json對象可使用JSON.NET或者FastJson等第三方類庫來實現。
================
參考資料
http://en.wikipedia.org/wiki/POST_(HTTP)
http://www.w3.org/TR/html4/interact/forms.html
http://en.wikipedia.org/wiki/XMLHttpRequest