2017-01-08 7 views
4

this questionなどの症状に似た症状がありますが、回答が役に立たなかったです。私は単純なHTMLフォームを介してノードアプリケーションにパスワードを送信しようとしていますが、リクエスト本体は空のまま戻ってきます。HTMLフォームを使用してPOSTリクエストを行うときにリクエスト本文が空です

サーバー:

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

router.post('/login', (req, res) => { 
    console.log(req.body); 
    console.log(req.headers['content-type']); 
}); 

フォーム:

<form action="/login" method="post"> 
    <input type="password" id="password"> 
    <button type="submit">Log In</button> 
</form> 

私はフォームを送信した場合、私は次を得る:

{} // req.body 
'application/x-www-form-urlencoded' // req.headers['content-type'] 

しかし、私はエンドポイントをカールしようとすると、私は空ではないreq.body

$ curl -X POST localhost:5000/login -d 'password=testpw' 

// Output 
{ password: 'testpw' } 
'application/x-www-form-urlencoded' 

私は間違っていますか?

+0

どのバージョンであるべき持っていないのですか? – Alex

+0

Expressバージョン4 – ericgio

答えて

6

問題は、フォームで

<form action="/login" method="post"> 
    <input type="password" id="password"> 
    <button type="submit">Log In</button> 
</form> 

であるあなたのinputname要素

は特急の

<input name="password" type="password" id="password"> 
+0

Ughhhhhhhhh。ありがとう。 – ericgio

+0

問題なし、簡単に間違いを作る! :) – Alex

関連する問題