http - $http.get() 与 JSON 数据

标签 http angularjs

我正在编写一个服务器应用程序,并希望客户端使用正文中的数据来参数化我的 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 })

这是设计使然还是错误?

我无法从 documentation 中看出原因.

最佳答案

我会把这个作为答案:

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

引用:HTTP GET with request body

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

引用:https://xhr.spec.whatwg.org/#the-send()-method

关于http - $http.get() 与 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16210347/

相关文章:

javascript - 将更新绑定(bind)到链接内的指令元素,而不是内联 <el someattr ="{{val}}"></el>

javascript - ng-click 后 Angular 不会重置表单?

ios - 将 curl 命令转换为 AFNetworking 调用

java - 如何使用 Java 编写 Web 服务

javascript - 从浏览器加载文件,无需网络服务器

javascript - 更改从单一来源动态添加的类似 ng-repeat 值

http - IIS中如何调整请求队列超时

python - 编写 Python 音乐流媒体

javascript - 带有 scope.$watch 的 Angular 指令强制验证其他字段

jquery - 运行 'ng test'命令时找不到JQuery