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

后端 未结 4 1722
忘了有多久
忘了有多久 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:15

    If you want to pass some data on using GET method you can pass a params options on the get method of the $http service. This will be urlencode parameters

    $http.get(url, {
      params: {
        query: 'hello world'
      }
    } 
    

    or

    $http({
       url: url,
       method:'GET',
       params: {
         query:'Hello World'
       }
    })
    

    But, the http standars define the POST method to send data to the server. The GET is just to obtain data from it. On angular the post method:

    $http({
      url: url,
      method:'POST',
      data: {
        query: 'Hello World'
      }
    })
    

    check the official docs from GET and POST

提交回复
热议问题