Sabarish Sankar Sabarish Sankar - 4 months ago 9
AngularJS Question

AngularJS passing data to $http.get request

I have a function which does a http POST request. The code is specified below. This works fine.

$http({
url: user.update_path,
method: "POST",
data: {user_id: user.id, draft: true}
});


I have another function for http GET and i want to send data to that request. But i dont have that option in get.

$http({
url: user.details_path,
method: "GET",
data: {user_id: user.id}
});


The syntax for http.get is

get(url, config)

Can someone help me with this.

Answer

A HTTP GET request can't contain data to be posted to the server. However you can add a query string to the request.

angular.http provides an option for it params.

$http({
    url: user.details_path, 
    method: "GET",
    params: {user_id: user.id}
 });

See: http://docs.angularjs.org/api/ng.$http#get and https://docs.angularjs.org/api/ng/service/$http#usage (shows the params param)

Comments