使用Angular的$ http的PUT方法时,向查询字符串添加参数

tro*_*man 22 asp.net-web-api angularjs

我正在使用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" }
})
Run Code Online (Sandbox Code Playgroud)

但是,当我使用PUT方法时,params是JSON编码的并作为请求有效负载发送:

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

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

Mic*_*zos 37

$http.put,$http.post或者$http.patch,该配置含有网址参数对象超出作为第三个参数,所述第二参数是所述请求体:

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

$http.put 文件供参考