工作中,最常用的两个HTTP的Method:GET、POST。用大白话说,GET用于“获取”数据(即“读”),POST用于“提交”数据(即“写”)。当然,这样的区别仅是就“用途”方面,也是“软”规则(实际上如果Server端实现比较“二”的话,GET方法也可能有“改变数据”的操作)。
从“协议”角度(浏览器等的实现)来说(即“硬”规则),二者的主要区别有:
- GET请求所带数据只能放在URL中,而POST的则可以放在Request Body中 [关键区别]
- GET请求所带数据长度有限制,POST无限制
因此,有几个常见的迷惑就有答案了:
- 上传图片,只能用POST(把图片数据写在URL中?一般人不这么干)
- jsonp不支持POST方法 (jsonp本就是GET方法实现的,还怎么POST咧? more...)
GET
The GET method requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect. (This is also true of some other HTTP methods.) The W3C has published guidance principles on this distinction, saying, "Web application design should be informed by the above principles, but also by the relevant limitations."
POST
The POST method requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI. The data POSTed might be, for example, an annotation for existing resources; a message for a bulletin board, newsgroup, mailing list, or comment thread; a block of data that is the result of submitting a web form to a data-handling process; or an item to add to a database.