2016-08-01 95 views
1

我是一個有點問題的初學者。我似乎無法將我的表單中的文本傳遞到隨後使用body-parser獲得的req.body中。關於這個問題有很多問題,但迄今爲止似乎沒有任何幫助。請指導我正確的方向。 MODS請不要鎖定。我已經搜索並可以找到合適的答案。謝謝。使用Angularjs輸入表單數據,Expressjs,

快遞:

app.use(bodyParser.urlencoded({'extended' : 'true'})); 

app.use(bodyParser.json()); 

app.use(bodyParser.json({type: 'application/vnd.api+json'})); 
app.use(methodOverride()); 




app.post('/', function(req, res){ 
     console.log(req.body.text); 
     res.redirect('https://google.com'); 
     res.end(); 

角:

angular.module('application', []) 

.controller('mainController', function($scope, $http) { 

    $scope.formData = {}; 

    $scope.submitForm = function() { 
    $http.post('/', $scope.formData) 
      .success(function(data) { 
       console.log(data); 
      }); 
    }; 
} 

HTML:

<form method="post" action="/"> 
       <div class="form-group"> 


        <input type="text" class="form-control input-lg text-center" placeholder="email" ng-model="formData.text"> 
       </div> 


       <button type="submit" class="btn btn-primary btn-lg" ng-submit="submitForm()">Add</button> 
      </form> 
+0

有你截獲'post'電話?它看起來像沒有實際的網址 – AranS

+0

請解釋。我不知道我需要一個。我以爲一個「/」會做 – Stephen

+0

我現在已經改變了網址,並得到一個404不能POST /錯誤 – Stephen

回答

0

你顯然不發送對象text到你的服務器,你發送一個formData對象包含一個text屬性。

你可以試試:

console.log(req.body.formData.text); 

然後,如果你想找回裏面的角的一些數據,你將數據發送回:

app.post('/', function(req, res){ 
    console.log(req.body.formData.text); 
    res.send("blablabla"); 

res.json({response: 'response'}); 
+0

這不起作用,但它可能會有所幫助。當我登錄(req.body.formData.text)時,我得到一個500服務器typeerror。 「TypeError:無法讀取C:\ Users \ Stephen \ Dropbox \ apps \ serverv1 \ server.js中未定義的 的屬性'文本':27:34 – Stephen