1. 程式人生 > >四種常用的post數據提交方式

四種常用的post數據提交方式

multi jquer web 調試 tle text 自動識別 -h lar

application/x-www-form-urlencoded

  這是默認的post傳輸方式,用url轉碼的方法,讓數據以key1=val1&key2=val2的方式傳輸。此方式的數據形式與get方式一樣。

multipart/form-data

  這個也是常見的方式,最常用於傳輸圖片和其他文件。下面是一段數據事例:

POST http://www.example.com HTTP/1.1
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA

------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="text"

title
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="file"; filename="chrome.png"
Content-Type: image/png

PNG ... content of chrome.png ...
------WebKitFormBoundaryrGKCBY7qhFd3TrwA--

  其中的的“------WebKitFormBoundaryrGKCBY7qhFd3TrwA”是用來分割數據的邊界,是瀏覽器自動生成的隨機字符串,傳輸到後臺時會自動識別為邊界,不需要作特別處理。此方式對文件有良好的轉碼,體積比較小。

application/json

  Angular中默認以這種方式傳輸。Jquery的ajax傳輸時需要把data對象JSON序列化,調用JSON.stringify(data)。一般的後臺語言對這種方式都支持,且JSON格式有利於調試工具的查看,推薦使用這種方式。

text/xml

  臃腫且不好調試,比較少使用。

推薦閱讀文章:四種常見的 POST 提交數據方式

四種常用的post數據提交方式