在Angular的$ http中使用PUT方法时,向查询字符串中添加参数 [英] Add parameters to query string when using PUT method with Angular's $http

查看:205
本文介绍了在Angular的$ http中使用PUT方法时,向查询字符串中添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular的$http服务来发出Web api请求.当我使用GET方法时,两个参数值将添加到查询字符串中:

I'm using Angular's $http service to make web api requests. When I use the GET method, the two param values are added to the query string:

// http://foo.com/api/test?heroId=123&power=Death+ray
$http.get("/api/test", {
   params: { heroId: 123, power : "Death ray" }
})

但是,当我使用PUT方法时,参数将进行JSON编码并作为请求有效负载发送:

However, when I use the PUT method the params are JSON-encoded and sent as the request payload:

// {"params":{"heroId":123,"power":"Death ray"}}
$http.put("/api/test", {
   params: { heroId: 123, power : "Death ray" }
})

使用PUT时如何强制将参数添加到查询字符串中?

How can I force the params to be added to the query string when using PUT?

推荐答案

对于$http.put$http.post$http.patch,包含您的url参数的 config 对象与第三个参数,第二个参数是请求正文:

With $http.put, $http.post or $http.patch, the config object containing your url parameters goes as the third argument, the second argument being the request body:

$http.put("/api/test",                                       // 1. url
          {},                                                // 2. request body
          { params: { heroId: 123, power : "Death ray" } }   // 3. config object
);

$http.put 文档以供参考

$http.put documentation for reference

这篇关于在Angular的$ http中使用PUT方法时,向查询字符串中添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆