将json数据发送到angularjs中的WebAPI方法

本文关键字:angularjs 中的 WebAPI 方法 json 数据 | 更新日期: 2023-09-27 18:06:14

$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);
    }

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) {                
     });

我的代码有什么问题?

将json数据发送到angularjs中的WebAPI方法

刚刚测试了这个,工作正常:

[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);

得到什么错误响应?可能是诸如在请求中不包括授权头,并且由于Api端点上的Authorize属性而获得401之类的事情?