AngularJS passing data to $http.get request

前端 未结 7 785
无人及你
无人及你 2020-11-22 13:01

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\",
   d         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 13:07

    Solution for those who are interested in sending params and headers in GET request

    $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;
       }
    
    });
    

提交回复
热议问题