简体   繁体   中英

$http in Angular how to post from form data in to nodejs

I have this form and the data is not appear they do not pass to my back end :

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

Now in my controller in script.js is this:

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

}]);

Now i wand to pass my data from my form to the nodejs in my server i have this code:

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

You call ng-submit="submit()" when you post the form but there is no submit() method in the searchControllers scope. Add it like this

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

Assuming you're using Express . Then the data should be in req.body .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM