angular form submit get action append key value pairs

333 Views Asked by At

My goal was to have off a form submit for a get to be performed and as part of the uri, key value pairs appended like the following: // GET /pets/42;q=11;r=22 Reading http://www.w3schools.com/tags/att_form_method.asp description of form with setting form method to "get" I would have thought this was possible. In the world of angular, it seems like the expected behavior for a submit of a form results in post method. The controller can be written to generate any http verb but has the $http.get doesn't do what plain form get method would automatically and to generate a url like above, the controller would have to build the uri itself. My apprehension of always using post off a form submit was the form in this case was part of a query/search action and not a data creation exercise. Maybe that's not the proper way to use form but has angular done away with automatically appended values from controls in key value pairs for a form with the $http.get service?

1

There are 1 best solutions below

0
On

Of course you can do GET with query params in url.

The easiest way is to pass an object representing the key/value pairs to the params property of the $http config object.

var params = {q: 11, r:22};
$.get('/path/to/server/', {params: params}).then(....

Alternatively you can also create your own url string when you only have small number of params.

For further details read through the Usage section of $http docs