gpt4 book ai didi

angularjs - 当我尝试从 Angular.js 发送到 Node.js 时,数据单独丢失

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:18 26 4
gpt4 key购买 nike

我正在尝试将数据从我的 Angular.js Controller 发送到 Node.js 后端。当请求被提出时,我成功地创建了一个 MongoDB 条目。但是 MongoDB 条目中的数据丢失了。我被卡住了,不能再继续我的应用程序了。谁能给我一个明确的解释,为什么我无法将表单数据发送到 Node.js

我把我的数据模式放在这里:

var common = require('../common');

var inviteeSchema = common.Schema({
email: String
});
var Invite = common.conn.model('Invite', inviteeSchema);
module.exports = Invite;

我在这里附上了路由代码。

router.route('/addtoinvitelist').post(function (req, res, next) {

var invite =new Invite(req.body);

invite.save(function(err,email){
if(err) throw err;

res.json({message:"your mail id is stored in our database we will soon send you an invite"})
});
});

我的 HTML 表单在这里

<form action="#">
<div class="form-group">
<label for="subcribeNewsletter" class="control-label">INVITE FORM<br> <small>We are happy to invite you to medicoshere, So please enter your email ID in the below form.</small></label>
<div class="input-group input-group-in input-group-sm">
<div class="input-group-addon"><i class="fa fa-envelope text-belpet"></i></div>
<input class="form-control" id="subcribeNewsletter" placeholder="name@mail.com" ng-model="useremail" required>
<div class="input-group-btn">
<button type="submit" class="btn btn-default text-belpet" ng-click="AddToInviteList(useremail)"><strong>OK</strong></button>
</div>
</div>
</div><!-- /.form-group -->
</form><!-- /form -->

我的 angular.service 功能在这里`this.AddToInviteList = function (email, cb) {

    $http({

method: 'POST',
url: "http://localhost:3000/users/addtoinvitelist",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}, // set the headers so angular passing info as form data (not request payload)
data:"email"

}).success(function (data) {

console.log("email is posted sucessfully" + data);
cb(data);
})

}`

Controller 函数在这里

App.controller('InviteController', function ($scope, $rootScope, $routeParams, $location, UserServices) {
$scope.init = function () {
console.log("hii this is a test ")
};


$scope.email = {};

$scope.AddToInviteList = function () {

UserServices.AddToInviteList($scope.email, function (dataresponse) {


console.log(dataresponse);

})

}

});

最佳答案

将电子邮件作为 json 对象传递 { 'email' : email }。

试试这个代码:

 $http({
method: 'POST',
url: "http://localhost:3000/users/addtoinvitelist",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data:{ 'email' : email }

}).success(function (data) {

console.log("email is posted sucessfully" + data);
cb(data);
})

Controller :

App.controller('InviteController', function ($scope, $rootScope, $routeParams, $location, UserServices) {
$scope.init = function () {
console.log("hii this is a test ")
};

$scope.AddToInviteList = function () {
$scope.user = {
email : $scope.useremail
};

UserServices.AddToInviteList($scope.user, function (dataresponse) {
console.log(dataresponse);
})
}
});

在服务器端,您可以通过调用“req.body.email”来访问电子邮件

关于angularjs - 当我尝试从 Angular.js 发送到 Node.js 时,数据单独丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com