Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 don't 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)

like image 224
Chubby Boy Avatar asked Dec 07 '12 09:12

Chubby Boy


People also ask

How do we pass data and get data using HTTP in angular?

get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });

How send parameters in HTTP GET request?

To use this function you just need to create two NameValueCollections holding your parameters and request headers. Show activity on this post. You can also pass value directly via URL. If you want to call method public static void calling(string name){....}

Which object does the HTTP GET () function return?

response : interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.

What is HTTP get in AngularJS?

In angularjs get ($http. get()) service or method is used to get data from remote HTTP servers. In angularjs get is a one of the shortcut method in $http service. In other ways, we can say that $http. get service or method in $http service is used to get data from the given URI.


2 Answers

An 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 called 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)

like image 80
fredrik Avatar answered Sep 21 '22 19:09

fredrik


You can pass params directly to $http.get() The following works fine

$http.get(user.details_path, {     params: { user_id: user.id } }); 
like image 43
Rob Avatar answered Sep 21 '22 19:09

Rob