How to send POST in angularjs with multiple params?
Client Side
Data needs to be grouped in an object array as payload - Indata
:
var Indata = {'product': $scope.product, 'product2': $scope.product2 };
Pass the payload through $http.post
as the second argument:
$http.post("http://localhost:53263/api/Products/", Indata).then(function (data, status, headers, config) {
alert("success");
},function (data, status, headers, config) {
alert("error");
});
Server Side
Create a Data Transfer Object(DTO) class as such:
public class ExampleRequest {
public string product {get; set;};
public string product2 {get; set;};
}
The class below accepts DTO with the same property names which the payload is carrying.
public void Post(ExampleRequest request)
{
var productRepository = new ProductRepository();
var newProduct = productRepository.Save(request.product);
}
In above class, request
contains 2 properties with values of product
and product2
Consider a post url with parameters user and email
params object will be
var data = {user:'john', email:'[email protected]'};
$http({
url: "login.php",
method: "POST",
params: data
})