$ http.post正在向RestApi发送空数据

时间:2014-05-07 10:54:06

标签: asp.net-mvc angularjs

我在我的应用程序中使用AngularJS,当我尝试将数据发布到RestAPI时,它会发送空数据

这是我的API函数

 [HttpPost]
    public string UpdateEmployee(Employees emp)
    {
using (_session = MvcApplication.Store.OpenSession())
        {
            var query = _session.Load<Employees>(emp.Id);
            query.JobTitle = emp.JobTitle;
            query.Name = emp.Name;
            query.Owner = emp.Owner;
            query.Phone = emp.Phone;
            query.SearchedByName = emp.SearchedByName;
            query.CanLogin = emp.CanLogin;
            query.Gender = emp.Gender;
            query.About = emp.About;
            query.Email = emp.Email;
            _session.SaveChanges();
        }
        return emp.Id;
    }

AngularJS代码:

var updateEmployee = function(emp) {
        var deffered = $q.defer();
        var data = JSON.stringify(emp);
        console.log(emp);
        console.log(data);


   $http({
          method: 'POST',
          url: '/api/Servicesapi/UpdateEmployee/',
          data: emp,
          headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
            }).success(
                function() {
                    deffered.resolve();
                })
            .error(
                function() {
                    //Error
                    deffered.reject();
                });
        return deffered.promise;
    };

第一个console.log(emp)打印数据,但第二个console.log(data)打印空数组[]

当上述角度函数执行但是接收到API的空对象时,会触发Employees函数。

console.log(emp)

About: "hello this is about"
Bid: "businesses/129"
CanLogin: true
Email: "abc@yahoo.com"
Gender: "Male"
Id: "employees/193"
JobTitle: "Developer"
Name: "abc"
Owner: true
Phone: "+123456789"
SearchedByName: true
Service: Array[0]
length: 0
__proto__: Array[0]

1 个答案:

答案 0 :(得分:2)

问题是您已将记录创建为数组并将其用作对象。

var record = [];

使用

var record = {};