简体   繁体   English

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

[英]$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: 现在在我的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);
    });

}]);

Now i wand to pass my data from my form to the nodejs in my server i have this code: 现在我想将数据从表单传递到服务器中的nodejs,我有以下代码:

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. 发布表单时,您调用ng-submit="submit()" ,但searchControllers范围中没有submit()方法。 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 . 假设您正在使用Express Then the data should be in req.body . 那么数据应该在req.body中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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