$ http.get()和JSON数据数据、http、get、JSON

2023-09-13 03:00:44 作者:Smile°凉城丶

我正在写一个应用程序服务器,并希望客户端使用的数据在身体pararmeterize我GET方法,如:

I'm writing a server app and wanted the client to use data in body to pararmeterize my GET method, like this:

# 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我尝试:

In AngularJS I tried:

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 })

这设计还是错误是?

我不能看到从文档为什么。

推荐答案

我想借此作为答案:

这不是禁止的,但你不应该使用它作为服务器可能(和应该)忽略 GET 请求的主体。

it's not prohibited, but you shouldn't use it as the server may (and SHOULD) ignore the body of GET request.

参考: HTTP GET请求与身