如何将数据从角度帖子传递到web api帖子

时间:2015-05-28 07:39:12

标签: javascript angularjs asp.net-web-api

我有一个角度post方法,通过它我想将数据传递给web api post但似乎不起作用。 我能做错什么?

customerPersistenceService.js

var customerPersistenceService = function ($http, $q) {
    return {
        save: function(customer) {
            var deferred = $q.defer();

            $http.post("/api/customers", customer)
                 .success(deferred.resolve)
                 .error(deferred.reject);

            return deferred.promise;
        },

        update: function(customer) {
            var deferred = $q.defer();

            $http.put("/api/customers/{id}" + customer.id, customer)
                 .success(deferred.resolve)
                 .error(deferred.reject);

            return deferred.promise;
        }
    };
};

customerEditCtrl.js

function customerEditCtr($stateParams, $location, customerPersistenceService) {
    var vm = this;
    vm.editableCustomer = {};
    vm.selectedCustomerId = $stateParams.id;
    customerPersistenceService.getById(vm.selectedCustomerId).then(
        function (customer) {
            vm.editableCustomer = customer;
        });
    };

    vm.saveCommand = function () {
        if (saveCustomer) {
            var customer = vm.editableCustomer;
            customer.id = vm.selectedCustomerId;

            if (customer.id !== 0) {
                customerPersistenceService.update(customer).then(
                    function (result) {
                        return result.data;
                    });

            } else {
                customerPersistenceService.save(customer).then(
                    function (result) {
                        return result.data;
                    });
            }
        }
    };
};

CustomerAPIController.cs 中,我的方法如下:

[HttpPost]
public HttpResponseMessage Post([FromBody]Customer newCustomer)
{
    try
    {
        if (ModelState.IsValid)
        {
            _customerService.AddNewCustomer(newCustomer);
            return Request.CreateResponse(HttpStatusCode.Created, newCustomer);
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
}


[HttpPut]
public HttpResponseMessage Put(int id, [FromBody]Customer editableCustomer)
{
    if (!ModelState.IsValid)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
    }

    if (id != editableCustomer.Id)
    {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }

    try
    {
        _customerService.UpdateCustomer(editableCustomer);
        return Request.CreateResponse(HttpStatusCode.OK, "{success:'true', verb:'PUT'}");
    }
    catch (DbUpdateConcurrencyException ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
}

更新 经过一番调查后,我意识到 vm.editableCustomer 似乎包含所有客户对象的数组,这使得很难传递给Web API POST。 我无法理解如何为所有客户对象分配此对象。

1 个答案:

答案 0 :(得分:2)

有一个明显的错误:

$http.put("/api/customers/{id}" + customer.id, customer)

这会尝试PUT到这样的网址:

http://yoursite/api/customers/{id}927

您需要删除{id}部分,以便您的网址正确无误:

http://yoursite/api/customers/927

括在大括号中的段只存在于服务器端路由模板中,它用于从传入的URI中提取参数。