我使用Angular的$http
服务来制作网络API请求。当我使用GET方法时,两个参数值被添加到查询字符串中:
// http://foo.com/api/test?heroId=123&power=Death+ray
$http.get("/api/test", {
params: { heroId: 123, power : "Death ray" }
})
但是,当我使用PUT方法时,params是JSON编码的并作为请求有效负载发送:
// {"params":{"heroId":123,"power":"Death ray"}}
$http.put("/api/test", {
params: { heroId: 123, power : "Death ray" }
})
如何在使用PUT时强制将参数添加到查询字符串中?
答案 0 :(得分:35)
使用$http.put
,$http.post
或$http.patch
,包含您的网址参数的 config 对象将作为第三个参数,第二个参数是请求体:
$http.put("/api/test", // 1. url
{}, // 2. request body
{ params: { heroId: 123, power : "Death ray" } } // 3. config object
);
$http.put
documentation以供参考
答案 1 :(得分:-2)
AngularJS发送json数据而不是x-www-form-urlencoded格式数据。 虽然您可以尝试下面的一个:
$http.put("/api/test", { heroId: 123, power : "Death ray" });
答案 2 :(得分:-2)
如果你的api url是“api / test / heroId / power”,
var data = 123 +'/ Death ray'
<强> $ http.put( “API /测试” +数据); 强>