我刚刚设法让我的$ http帖子在Angular上工作。它做了它需要做的事情,但是当我发布数据然后刷新页面时,它在我的控制台中出现了这个错误:
可能未处理的拒绝:{“data”:null,“status”: - 1,“config”:{“method”:“POST”,“transformRequest”:[null],“transformResponse”:[null], “jsonpCallbackParam”: “回调”, “URL”: “/插入”, “数据”:
这是我的角色职位:
app.controller('ContactCtrl', function($scope, $http){
$scope.formModel = {};
$scope.onSubmit = function(){
$http.post('/insert', $scope.formModel)
console.log('success');
};
});
这是我在Express.js中对MongoDB的服务器调用:
router.post('/insert', function(req, res, next){
var item = {
name: req.body.name,
adress: req.body.adress,
postal: req.body.postal,
city: req.body.city,
email: req.body.email,
phone: req.body.phone,
quotation: req.body.quotation,
message: req.body.message
};
var data = new UserData(item);
data.save();
console.log('Item inserted');
});
答案 0 :(得分:2)
尝试
var BACKEND_URL = "http://whatever:port";
var postData = angular.toJson($scope.formModel, true);
$http.post(BACKEND_URL + '/insert', postData);
并在后端,在mongo保存操作后使用res.sendStatus(200);
或修改如下。
.save(function(err, result) {
if (err) throw err;
if(result) {
res.json(result)
}
})