[英]Angular http.post sending empty json
i have application on AngularJS and NodeJS on the back, when I put my app on VPS, Angular is sending a post with json but object is completely empty, fields are undefined. 我在后面有AngularJS和NodeJS的应用程序,当我把我的应用程序放在VPS上时,Angular正在发送一个带有json的帖子,但是对象是完全空的,字段是未定义的。 Here is my code:
这是我的代码:
This is my controller: 这是我的控制器:
app.controller('fullAppController', ['$scope','$http','$location','$cookies',function($scope,$http,$location,$cookies)
My config to provide default options for post: 我的配置为post提供默认选项:
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.headers.post = {};
}]);
My problem: 我的问题:
$scope.registerSubmitData = function(){
if(propertyCheck($scope.registerData,'userName') &&
propertyCheck($scope.registerData,'email') &&
propertyCheck($scope.registerData,'password') &&
propertyCheck($scope.registerData,'passwordCheck') &&
propertyCheck($scope.registerData,'firstName') &&
propertyCheck($scope.registerData,'lastName') &&
propertyCheck($scope.registerData,'street') &&
propertyCheck($scope.registerData,'streetNum') &&
propertyCheck($scope.registerData,'postCode') &&
propertyCheck($scope.registerData,'city') &&
propertyCheck($scope.registerData,'state')){
console.log($scope.registerData); //HERE IS FILLED OBJECT
$http.post('http://localhost:8000/register', $scope.registerData).success(function(data,status){
// HERE ANGULAR POST A EMPTY JSON
$location.path('/login');
$scope.logInSuccesAlert = true;
$scope.logInSuccesAlertText = "Successfully register";
}).catch(function(err){
$scope.registerAlert = true;
$scope.registerAlertText = "Username exist";
});
}else{
$scope.wrongNoState = true;
$scope.registerAlert = true;
$scope.registerAlertText = "Please fill all fields";
$scope.wrongNoStateText = "Please select state";
}
}
(I'm using cors)My NodeJS receiving: (我正在使用cors)我的NodeJS接收:
app.use('/register', function(req,res){
console.log(req.body);
//HERE RECEIVED JSON (req.body) IS EMPTY
var registerData = req.body;
registerData.password = SHA256(registerData.password);
insertUser(registerData).then(function(result){
res.send("Successfully register");
}).catch(function(err){
res.status(409).end("User exist");
});
});
How to handle this problem ? 怎么处理这个问题?
Try replacing: 尝试更换:
$http.post('http://localhost:8000/register', $scope.registerData).success(function(data,status){
with: 有:
$http.post('http://localhost:8000/register', $scope.registerData, { headers: { 'Content-Type': 'application/json' } }).success(function(data,status){
You should specify the Content-Type. 您应该指定Content-Type。 If you want all your $http.post to be json you may be able to add that in your default options.
如果您希望所有$ http.post都是json,您可以在默认选项中添加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.