How to do a $http GET with some data in AngularJS?

后端 未结 4 1689
忘了有多久
忘了有多久 2021-01-04 21:26

I want to make a Get request to my Backend, but I want to verify some user credentials before sending the response object.

Here is my code:

$scope.ge         


        
4条回答
  •  花落未央
    2021-01-04 22:20

    You're most likely going to need to use a POST method instead of a GET method.

    But to do it with a GET method:

    From AngularJS passing data to $http.get request:

    A 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 params.

    $http({
         url: user.details_path, 
         method: "GET",
         params: {user_id: user.id}  
    });
    

    And using AngularJS to POST instead:

    $http.post('/someUrl', {msg:'hello word!'}).
      then(function(response) {
        // this callback will be called asynchronously
        // when the response is available
      }, function(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    

提交回复
热议问题