Reputation: 4100
$scope.items = {2: true, 4: true, 5: true, 7: true, 9: true, 10: true, 11: true };
How do I post the above json data to the following WebAPI method using angularjs's $http?
[Authorize]
[HttpPost]
[Route("moveemployees")]
public HttpResponseMessage MoveEmployees(Dictionary<decimal, bool> employeeList)
{
// employeeList doesn't contain any items after the post
return Request.CreateResponse(HttpStatusCode.OK);
}
I tried :
$http({
method: 'POST',
cache: false,
url: $scope.appPath + 'Employee/moveemployees',
data: { employeeList : $scope.items },
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).success(function (data, status) {
$scope.infos.push('Employees Moved Successfully');
$scope.errors = [];
}).error(function (data, status) {
});
What's wrong with my code?
Upvotes: 5
Views: 6910
Reputation: 17492
Just tested this, works fine:
[Route("moveemployees")]
public void Post(Dictionary<decimal, bool> employeeList)
{
}
and:
$scope.items = { 2: true, 4: true, 5: true, 7: true, 9: true, 10: true, 11: true };
var config = {
method: "POST",
url: "moveemployees",
data: $scope.items
};
$http(config);
What error response are you getting? Might be something like not including an authorization header in your request and getting a 401 due to the Authorize attribute on you Api endpoint?
Upvotes: 5