带有JSON数据的$ http.get()

lee*_*sei 11 http angularjs

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

# 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"
}
Run Code Online (Sandbox Code Playgroud)

在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 })
Run Code Online (Sandbox Code Playgroud)

这是设计还是错误?

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

lee*_*sei 13

我会以此为答案:

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

参考:带请求正文的HTTP GET

对于XHR,身体GETHEAD将被忽略(由@ jacob-koshy暗示).

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