我使用邮差将数据发布到后端,它工作得很好。使用相同的api,我试图通过角发布数据给我的问题。但我明白,我的form
数据与post
过程之间发生了错误,这是我无法理解的。
下面是post函数:
.post(function( req, res ){
console.log(' called ', req.body ); 1. //getting all properties
var family = new Family();
family.username = req.body.username,
family.password = req.body.password,
family.familyLeader = req.body.familyLeader,
family.husband = req.body.husband,
family.wife = req.body.wife,
family.kids = req.body.kids;
console.log( 'family username', req.body.username ); 2.//undefined? not getting
family.save(function( err, newFamily ) {
if( err ) {
if ( err.code == 11000) {
return res.json({ success: false, message: 'A user with that username already exists. '});
}
else {
return res.send( err );
}
}
res.json({ message: 'Family created!', newFamily: newFamily });
});
})
有2个控制台我在上面的呼叫,在第一个控制台我得到这样的属性:(我想这是问题,属性被标记!?)
called { '{"username":"arif","password":"sadfdsa","familyLeader":"sadffsa","husband":"sadfsad","wife":"sadfsad","kids":2}': '' }
和下一个控制台给出如下值:
family username undefined
我已经从$http
请求中删除了头,它工作得很好。
我的代码问题是:与头
vm.createNewFamily = function() {
$http({
method : 'POST',
url : '/api/family',
data : vm.form,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).success( function ( data ) {
console.log('retured!', data );
})
}
,解决方案是:(header removed)
vm.createNewFamily = function() {
$http({
method : 'POST',
url : '/api/family',
data : vm.form
}).success( function ( data ) {
console.log('retured!', data );
})
}
专家-请确认解决方案!