我正在使用Angular的$http
服务来发出web api请求。当我使用GET方法时,两个参数值被添加到查询字符串中:
// http://foo.com/api/test?heroId=123&power=Death+ray
$http.get("/api/test", {
params: { heroId: 123, power : "Death ray" }
})
但是,当我使用PUT方法时,参数是json编码的,并作为请求有效负载发送:
// {"params":{"heroId":123,"power":"Death ray"}}
$http.put("/api/test", {
params: { heroId: 123, power : "Death ray" }
})
使用PUT时,如何强制将参数添加到查询字符串中?
对于$http.put
, $http.post
或$http.patch
,包含url参数的配置对象作为第三个参数,第二个参数是请求体:
$http.put("/api/test", // 1. url
{}, // 2. request body
{ params: { heroId: 123, power : "Death ray" } } // 3. config object
);
$http.put
参考文档
AngularJS发送json数据,而不是x-www-form-urlencoded格式的数据。不过你可以试试下面这个:
$http.put("/api/test", { heroId: 123, power : "Death ray" });
如果你的api url是"api/test/heroId/power",
var data = 123+'/Death ray'
http.put美元("api/测试"+数据);