Samantha J T Star
Samantha J T Star

Reputation: 32758

How can I send URL parameters to my server using $http.post with AngularJS?

I have the following code:

$http.post('/api/City?cityId=' + $scope.config.cityId)

This works and sends the expected URL to the server.

Now I would like to add more to the URL to send additional information. Is there a way I can define the details such as cityId in an object and have this sent? Note that I did try:

$http.post('/api/City', {
             params: {
                 cityId: $scope.config.cityId
             }
})

This has the effect of sending a JSON object containing cityId to the server which is not what I want in this case as my server is expecting this:

    [HttpPost]
    [ActionName("City")]
    public HttpResponseMessage City(int cityId, int locId, testId)

Upvotes: 0

Views: 159

Answers (1)

Roger
Roger

Reputation: 2952

The params object is a property of the third parameter to the $http.post function, not the second.

http://docs.angularjs.org/api/ng/service/$http#post

You can also use the long version

$http({
  method: 'POST',
  url: '/api/City'
  params: {...}
});

Upvotes: 1

Related Questions