$scope.items = {2: true, 4: true, 5: true, 7: true, 9: true, 10: true, 11: true };
如何使用angularjs的$ http将上述json数据发布到以下WebAPI方法?
[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);
}
我试过了 :
$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) {
});
我的代码有什么问题?
最佳答案
刚刚测试过,效果很好:
[Route("moveemployees")]
public void Post(Dictionary<decimal, bool> employeeList)
{
}
和:
$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);
您得到什么错误响应?可能类似于在您的请求中不包含授权 header ,以及由于Api端点上的Authorize属性而获得401之类的东西吗?