$ http in Angular如何从形式数据发布到nodejs



我有此表格,并且没有看出数据不会传递给我的后端:

<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>

现在在脚本中的控制器中。

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  
}); 

发布表单时调用 ng-submit="submit()",但是在搜索controllers范围中没有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);
        });
    };
}]);

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

最新更新