12

我正在编写一个服务器应用程序,并希望客户端使用正文中的数据来参数化我的 GET 方法,如下所示:

# http -v GET http://localhost:3000/url text=123 foo=bar
GET /url HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 29
Content-Type: application/json; charset=utf-8
Host: localhost:3000
User-Agent: HTTPie/0.4.0

{
    "foo": "bar", 
    "text": "123"
}

在 AngularJS 中,我尝试过:

var params = {
    "foo": "bar", 
    "text": "123"
}

// no body
$http({
  method: 'GET',
  url: '/url',
  data: params })

// ugly url
// also has its limitation: http://stackoverflow.com/questions/978061/http-get-with-request-body
$http({
  method: 'GET',
  url: '/url',
  params: params })

// params in body, but I wanted GET
$http({
  method: 'POST',
  url: '/url',
  data: params })

这是设计使然还是错误?

我无法从文档中看出原因。

4

1 回答 1

15

我会将此作为答案:

对于 HTTP,它没有被禁止,但你不应该使用它,因为服务器可能(并且应该)忽略GET请求的主体。

参考:带有请求正文的 HTTP GET

对于 XHR,GET和的主体HEAD将被忽略(@jacob-koshy 提示)。

参考:https ://xhr.spec.whatwg.org/#the-send()-method

于 2013-06-02T18:50:21.837 回答