Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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" }
})

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" }
})

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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 documentation for reference


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...