Javascript 不能在HTTP GET请求的body中包含参数
阅读(10483)进行RESTful API设计的时候,一般想将POST GET PUT DELETE所用到的参数,倾向于使用结构化数据来描述,如使用JSON或XML,若是放到HTTP BODY中看起来漂亮,Server端获取这些参数的方式也能一致。
但是,对于GET请求,不能将参数放在HTTP BODY中。
这里有个关注度很高的激烈讨论:http://stackoverflow.com/questions/978061/http-get-with-request-body
结论是:你可以在GET请求中带有HTTP BODY内容,但是服务端不应该解析它,否则有悖HTTP 1.1规范。
即算你自己开发的server支持了此种情况的解析,也不能保证其他server或代理服务器支持。
这是HTTP 规范的主要创作人之一 Roy T. Fielding 的观点:
Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.
So, yes, you can send a body with GET, and no, it is never useful to do so.
This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress).
....Roy
via: https://groups.yahoo.com/neo/groups/rest-discuss/conversations/messages/9962
另外,不是所有客户端都支持发起带有body的HTTP GET请求,比如jQuery就直接限制了: http://bugs.jquery.com/ticket/8961