如何在 angular/node 应用程序中正确发送和接收表单数据
how to properly send and receive form data in an angular/node application
我正在编写一个基于 mean.js 样板的应用程序
我的 angular 视图中有一个表单,需要发送一些表单数据:
<form name="form" ng-submit="postUpdate()">
<div class="form-group">
<fieldset>
<legend><strong>Salesforce Opportunity</strong> </legend>
<div class="col-sm-6">
<label for="opportunityId">Opportunity ID</label>
<input id="kw" name="opportunityId" type="text" placeholder="kw" class="form-control" ng-model="kwRequired"/>
......
在我的 angular 控制器中,我有这个:
$scope.postUpdate = function(){
var posturl = '/salesforce_update';
console.log('kwRequired ' + $scope.kwRequired);
var postData = {kw: $scope.kwRequired};
$http.post(posturl,postData);
}
然后,在我的服务器 node/express 代码中,我有这个处理程序:
....
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
......
app.post('/salesforce_update', function(req, res){
console.log('Salesforce update Request Received');
console.log('_parsedUrl.query: ' + req.body.kw);
});
我确实将请求发送到服务器端,但经过检查,请求正文是空的。
我错过了什么?
您需要使用这样的angular.toJson
方法:
$scope.postUpdate = function(){
console.log('kwRequired ' + $scope.kwRequired);
var postData = {kw: $scope.kwRequired};
var json = angular.toJson(postData);
$http.post('/salesforce_update', json);
}
还要确保在 app.post('/salesforce_update', function ....
的定义 之前定义 app.use(bodyParser.json());
我正在编写一个基于 mean.js 样板的应用程序
我的 angular 视图中有一个表单,需要发送一些表单数据:
<form name="form" ng-submit="postUpdate()">
<div class="form-group">
<fieldset>
<legend><strong>Salesforce Opportunity</strong> </legend>
<div class="col-sm-6">
<label for="opportunityId">Opportunity ID</label>
<input id="kw" name="opportunityId" type="text" placeholder="kw" class="form-control" ng-model="kwRequired"/>
......
在我的 angular 控制器中,我有这个:
$scope.postUpdate = function(){
var posturl = '/salesforce_update';
console.log('kwRequired ' + $scope.kwRequired);
var postData = {kw: $scope.kwRequired};
$http.post(posturl,postData);
}
然后,在我的服务器 node/express 代码中,我有这个处理程序:
....
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
......
app.post('/salesforce_update', function(req, res){
console.log('Salesforce update Request Received');
console.log('_parsedUrl.query: ' + req.body.kw);
});
我确实将请求发送到服务器端,但经过检查,请求正文是空的。
我错过了什么?
您需要使用这样的angular.toJson
方法:
$scope.postUpdate = function(){
console.log('kwRequired ' + $scope.kwRequired);
var postData = {kw: $scope.kwRequired};
var json = angular.toJson(postData);
$http.post('/salesforce_update', json);
}
还要确保在 app.post('/salesforce_update', function ....
app.use(bodyParser.json());