我在我的应用程序中使用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]
答案 0 :(得分:2)
问题是您已将记录创建为数组并将其用作对象。
var record = [];
使用
var record = {};