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)

Can someone help me with this?


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)


您可以直接将params传递给$http.get()以下工作正常

$http.get(user.details_path, {
    params: { user_id: user.id }
});

AngularJS v1.4.8开始,你可以使用get(url, config) ,如下所示:

var data = {
 user_id:user.id
};

var config = {
 params: data,
 headers : {'Accept' : 'application/json'}
};

$http.get(user.details_path, config).then(function(response) {
   // process response here..
 }, function(response) {
});
链接地址: http://www.djcxy.com/p/7278.html

上一篇: 查询字符串的最大可能长度是多少?

下一篇: AngularJS将数据传递给$ http.get请求