AngularJS将数据传递到$ http.get请求

本文翻译自:AngularJS passing data to $http.get request

I have a function which does a http POST request. 我有一个执行http POST请求的函数。 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. 我为http GET提供了另一个功能,我想将数据发送到该请求。 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 http.get的语法是

get(url, config) 获取(URL,配置)


#1楼

参考:https://stackoom.com/question/vjcc/AngularJS将数据传递到-http-get请求


#2楼

An HTTP GET request can't contain data to be posted to the server. HTTP GET请求不能包含要发布到服务器的数据。 However, you can add a query string to the request. 但是,您可以将查询字符串添加到请求中。

angular.http provides an option for it called params . angular.http为此提供了一个名为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) 请参阅: http : //docs.angularjs.org/api/ng.$http#gethttps://docs.angularjs.org/api/ng/service/$http#usage (显示params参数)


#3楼

You can pass params directly to $http.get() The following works fine 可以将参数直接传递给$http.get() ,以下方法可以正常工作

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

#4楼

You can even simply add the parameters to the end of the url: 您甚至可以简单地将参数添加到url的末尾:

$http.get('path/to/script.php?param=hello').success(function(data) {
    alert(data);
});

Paired with script.php: 与script.php配对:

<? var_dump($_GET); ?>

Resulting in the following javascript alert: 导致以下JavaScript警报:

array(1) {  
    ["param"]=>  
    string(4) "hello"
}

#5楼

Solution for those who are interested in sending params and headers in GET request 对于那些对在GET请求中发送参数和标头感兴趣的人的解决方案

$http.get('https://www.your-website.com/api/users.json', {
        params:  {page: 1, limit: 100, sort: 'name', direction: 'desc'},
        headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
    }
)
.then(function(response) {
    // Request completed successfully
}, function(x) {
    // Request error
});

Complete service example will look like this 完整的服务示例如下所示

var mainApp = angular.module("mainApp", []);

mainApp.service('UserService', function($http, $q){

   this.getUsers = function(page = 1, limit = 100, sort = 'id', direction = 'desc') {

        var dfrd = $q.defer();
        $http.get('https://www.your-website.com/api/users.json', 
            {
                params:{page: page, limit: limit, sort: sort, direction: direction},
                headers: {Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
            }
        )
        .then(function(response) {
            if ( response.data.success == true ) { 

            } else {

            }
        }, function(x) {

            dfrd.reject(true);
        });
        return dfrd.promise;
   }

});

#6楼

Starting from AngularJS v1.4.8 , you can use get(url, config) as follows: 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) {
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值