[英]AngularJS: Unable to receive data from $http.post()
I am able to: 我能够:
Problem: - From Service, I am not able to access my data in Server.js file and thus as a result I am not able to insert the data into MySQL table. 问题: -无法通过Service访问Server.js文件中的数据,因此无法将数据插入MySQL表。
My AngularJS Service here uses built-in angularJS $http service to post the data. 我的AngularJS服务使用内置的angularJS $ http服务发布数据。
I just started learning AngularJS, and came across this problem. 我刚开始学习AngularJS,就遇到了这个问题。
I have commented at appropriate places for ease. 为了方便起见,我已在适当的地方发表了评论。
HTML form code: HTML表单代码:
<form class="form-horizontal" ng-submit="submit()">
<label class="control-label col-sm-2" for="id">ID:</label>
<input type="text" class="form-control" id="id" placeholder="Enter ID" ng-model="text1">
<label class="control-label col-sm-2" for="title">Title:</label>
<input type="text" class="form-control" id="title" placeholder="Enter Title" ng-model="text2">
Controller code: 控制器代码:
(function() {
var newArticleController = function ($scope, customersService, appSettings) {
$scope.list = []; // will pass this list
//init function: customerService is a Service
function init() {
customersService.postArticle($scope.list)
.success(function() {
console.log('post promise success!') //This is not printing!!!!!
})
.error(function(data, status, headers, config) {
})
}
//on submit: init() is called
$scope.submit = function() {
if ($scope.text1 && $scope.text2) {
$scope.list.push(this.text1)
$scope.list.push(this.text2)
init()
} } };
angular.module('customersApp')
.controller('newArticleController', newArticleController);
}());
AngularJS Service code: AngularJS 服务代码:
(function(){
var customersService = function($http) {
this.postArticle = function(dataRcvd) {
console.log('service called');
console.log(dataRcvd); //This is printing
return $http.post('/newArticle', JSON.stringify(dataRcvd))
}
}
angular.module('customersApp').service('customersService', customersService);
}())
Server.js (problem comes in this part) Server.js (问题在此部分)
var express = require('express'),
var app = express();
var knex = require('knex')({
//...
});
app.use(express.static(__dirname + '/'));
app.post('/newArticle', function(req, res) {
console.log('reached here: server.js') //This is printing
---> **//HOW TO ACCESS 'dataRcvd' here ???**
})
I am a newbie, so if any edits are required then please help me out. 我是新手,所以如果需要任何修改,请帮帮我。
Try by updating your code as below. 尝试如下更新代码。
Step 1- Add the "body-parser" first. 步骤1-首先添加“ body-parser”。
var bodyParser = require('body-parser');
Step 2- and include middleware as 第2步,并将中间件作为
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
Step 3- Try to access data as follows. 步骤3-尝试按以下方式访问数据。
app.post('/newArticle', function(req, res) {
console.log('reached here: server.js') //This is printing
---> **//HOW TO ACCESS 'dataRcvd' here ???**
console.log(req.body);
res.end();
})
So, to resolve the promise at angular end please update your service code as follows. 因此,要兑现承诺,请按如下所示更新您的服务代码。
angular.module('customersApp').service('customersService', ['$http', '$q', function ($http, $q) {
this.postArticle = function(dataRcvd) {
var deferred = $q.defer();
console.log('service called');
console.log(dataRcvd); //This is printing
$http({
method: 'POST',
url: '/newArticle',
headers: {'Content-Type': 'application/json'},
data: JSON.stringify(dataRcvd)
}).success(function (data) {
deferred.resolve(data);
}).error(function (msg) {
deferred.reject(msg);
});
return deferred.promise;
}
}]);
Add body-parser to your server.js 将正文解析器添加到您的server.js
var express = require('express'),
var app = express();
**var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json**
Then you can use 那你可以用
req.body
to access the data. 访问数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.