Coding Duchess
Coding Duchess

Reputation: 6899

Is it possible to pass two parameters to $http.post from AngularJS

I do not have an issue constructing $http.post request with an object or single parameter, but in one particular case I need to pass 2 simple parameters, an integer and a string.

Normally I use:

 $http.post(url, id)
       .success(function (data, status, header) {
           $scope.result = data;

       })
       .error(function (data, status, header) {
           $scope.result = "Data: " + data +
               "<hr />status: " + status;
           console.log(header.responseText);
       });

But how can I modify it to pass two parameters?

I have tried creating an object with two properties and passing that object:

var inData = {
   id: id,
   param2: param2
}

$http.post(url, inData)
   .success(function (data, status, header) {
       $scope.result = data;

   })
   .error(function (data, status, header) {
       $scope.result = "Data: " + data +
           "<hr />status: " + status;
       console.log(header.responseText);
   });

But then I am not sure what to specify as a parameter in my Web API method that is passed in via url parameter.

Is that possible to pass in 2 separate parameters instead of passing in an object?

Upvotes: 0

Views: 4054

Answers (1)

georgeawg
georgeawg

Reputation: 48948

But then I am not sure what to specify as a parameter in my Web API method that is passed in via url parameter.

URL parameters are added to the $http.post method as part of the optional config object:

 var params = { name: value,
                name2: value2
              };
 $http.post(url, data, { params: params });

In the above example, the $http service will append ?name=value&name2=value2 to the URL. The data argument is placed in the body of the POST request. If the data argument is an object it will be transformed to a string using the default transformRequest function.

For more information, see AngularJS $http Service API Reference.

Upvotes: 1

Related Questions