2016-11-11 59 views
0
不發送數據

我在HTML格式如下:HTML形式快遞

<form method="post" id="registration-form" action="/register"> 
     <div class="form-group"> 
     <label for="UsernameRegistration">Username:</label> 
     <input type="text" class="form-control" id="UsernameRegistration"> 
     </div> 
     <div class="form-group"> 
     <label for="PasswordRegistration">Password:</label> 
     <input type="password" class="form-control" id="PasswordRegistration"> 
     </div> 
     <div class="form-group"> 
     <label for="ConfirmPasswordRegistration">Confirm Password:</label> 
     <input type="password" class="form-control" id="ConfirmPasswordRegistration"> 
     </div> 
     <input type="submit" class="form-control" /> 
    </form> 

/register端點如下所示:

router.post('/register', function(req, res, next) { 
    console.log(req); 
}); 

在req.query和req.body,有沒有數據。我究竟做錯了什麼?

回答

4
<input type="password" class="form-control" id="PasswordRegistration"> 

這裏沒有指定的屬性。 應該是這樣

<input type="password" name="password" class="form-control" id="PasswordRegistration"> 
2

您還沒有輸入內容提供者的名稱屬性。

我給你提供name屬性元素如:

在req.params
<form method="post" id="registration-form" action="/register"> 
     <div class="form-group"> 
     <label for="UsernameRegistration">Username:</label> 
     <input name="username" type="text" class="form-control" id="UsernameRegistration"> 
     </div> 
     <div class="form-group"> 
     <label for="PasswordRegistration">Password:</label> 
     <input name="password" type="password" class="form-control" id="PasswordRegistration"> 
     </div> 
     <div class="form-group"> 
     <label for="ConfirmPasswordRegistration">Confirm Password:</label> 
     <input name="confpass" type="password" class="form-control" id="ConfirmPasswordRegistration"> 
     </div> 
     <input type="submit" class="form-control" /> 
    </form> 

router.post("/registration", (req, res) => { 
    var username = req.params.username; 
    var pass = req.params.password; 
    var confpass = req.params.confpass; 
}) 

你將得到的數據對象。

0

我認爲你缺少這兩樣東西: -

1.Have您在我們的應用程序添加體解析器? (包含在請求體提交的數據的鍵 - 值對。默認情況下,它是未定義,並且當您使用身體解析等中間件體解析器填充)

var app = require('express')(); 

var bodyParser = require('body-parser'); 

app.use(bodyParser.json()); // for parsing application/json 
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded 
  • 缺少屬性,在表單元素