Angular中的$ http如何从表单数据发布到nodejs

时间:2017-09-01 12:56:05

标签: javascript angularjs node.js

我有这个表单,数据没有出现,他们没有传递到我的后端:

<div ng-controller="searchController">
<form ng-submit="submit()">
    <input type="text" name="name" ng-model="namegirl" />
    <input type="text" name="namewod" ng-model="namewod" />
    <input type="submit"/>
</form>

现在我在script.js的控制器中是这样的:

myApp.controller('searchController', ['$scope', '$filter', '$http', function ($scope, $filter, $http) {

    let data = {
        namegirl  :$scope.namegirl,
        name      :$scope.namewod
    };

    console.log(data);

    $http.post('/wodsearch',data)
    .success(function (response) {
        console.log(response.data);
    })
    .error(function (response, status) {
        console.log(response);
    });

}]);

现在我将我的数据从我的表单传递到服务器中的nodejs我有这个代码:

app.post('/wodsearch',function(req, res ,next){
    // how to get here the data from my angular  
}); 

2 个答案:

答案 0 :(得分:1)

发布表单时调用ng-submit="submit()",但searchControllers范围内没有submit()方法。像这样添加

myApp.controller('searchController', ['$scope', '$filter', '$http'
, function ($scope, $filter, $http) {

    $scope.submit = function (){
        let data = {
            namegirl  :$scope.namegirl,
            name      :$scope.namewod
        };

        console.log(data);

        $http.post('/wodsearch',data)
        .success(function (response) {
            console.log(response.data);
        })
        .error(function (response, status) {
            console.log(response);
        });
    };
}]);

答案 1 :(得分:0)

假设您正在使用Express。 然后数据应该在req.body