使用JSON数据的$http.get()

12

我正在编写一个服务器应用程序,并希望客户端使用body中的数据来参数化我的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: https://dev59.com/53NA5IYBdhLWcg3wZ85S
$http({
  method: 'GET',
  url: '/url',
  params: params })

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

这是设计还是一个bug?

文档中我看不出原因。


3
你确定接收端支持它吗?[带有请求正文的GET请求不被禁止,但也不被期望](https://dev59.com/53NA5IYBdhLWcg3wZ85S)。 - bzlm
2
忘记文档,去研究一下HTTP中GET的含义。 - 7stud
GET方法不支持请求体。 - Arun P Johny
5
我同意......你问题的答案是“使用POST”,但这不是一个确切的答案,我不想冒险影响我的声誉。 - boisvert
我见过的最好的解释是这个答案 - thprogrammer
显示剩余4条评论
1个回答

15

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接