2017-09-01 33 views
0

我有這樣的形式和數據不出現不傳遞給我的後端:

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

的可能的複製[如何消耗的Express應用程序的JSON POST數據(https://stackoverflow.com/questions/10005939/how-do-i-consume-the- json-post-data-in-express-application) –

回答

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

是的,我忘了提交。它工作正常 – erevos13

0

假設你正在使用Express。 然後數據應該在req.body

+0

是的,我使用快遞。但他們返回一個空的obj。 – erevos13

+0

使用app.use(express.bodyParser());'app.post(......)之前'' –