AngularJS passing data to $http.get request

前端 未结 7 688
无人及你
无人及你 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:04

    You can even simply add the parameters to the end of the url:

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

    Paired with script.php:

    <? var_dump($_GET); ?>
    

    Resulting in the following javascript alert:

    array(1) {  
        ["param"]=>  
        string(4) "hello"
    }
    
    0 讨论(0)
  • 2020-11-22 13:05

    For sending get request with parameter i use

      $http.get('urlPartOne\\'+parameter+'\\urlPartTwo')
    

    By this you can use your own url string

    0 讨论(0)
  • 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;
       }
    
    });
    
    0 讨论(0)
  • 2020-11-22 13:17

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

    $http.get(user.details_path, {
        params: { user_id: user.id }
    });
    
    0 讨论(0)
  • 2020-11-22 13:18

    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)

    0 讨论(0)
  • 2020-11-22 13:20

    Here's a complete example of an HTTP GET request with parameters using angular.js in ASP.NET MVC:

    CONTROLLER:

    public class AngularController : Controller
    {
        public JsonResult GetFullName(string name, string surname)
        {
            System.Diagnostics.Debugger.Break();
            return Json(new { fullName = String.Format("{0} {1}",name,surname) }, JsonRequestBehavior.AllowGet);
        }
    }
    

    VIEW:

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script type="text/javascript">
        var myApp = angular.module("app", []);
    
        myApp.controller('controller', function ($scope, $http) {
    
            $scope.GetFullName = function (employee) {
    
                //The url is as follows - ControllerName/ActionName?name=nameValue&surname=surnameValue
    
                $http.get("/Angular/GetFullName?name=" + $scope.name + "&surname=" + $scope.surname).
                success(function (data, status, headers, config) {
                    alert('Your full name is - ' + data.fullName);
                }).
                error(function (data, status, headers, config) {
                    alert("An error occurred during the AJAX request");
                });
    
            }
        });
    
    </script>
    
    <div ng-app="app" ng-controller="controller">
    
        <input type="text" ng-model="name" />
        <input type="text" ng-model="surname" />
        <input type="button" ng-click="GetFullName()" value="Get Full Name" />
    </div>
    
    0 讨论(0)
提交回复
热议问题