天天看點

關于我的router.post擷取為空的問題

我在學習express架構有一個錯很經典,就是我的req.body為空的問題。

廢話不多說,上示範代碼

index.js

router.post("/reg",function(req,res){
  console.log(req.body.name);
  res.redirect('/');
})
           

index.ejs

<body>
    <form method="POST" >
        <input type="text" id="name" /><hr>
        <button type="submit" >送出</button>
    </form>
</body>
           

使用上述代碼就會擷取為空,這是在學習node.js開發指南的時候類似上方的代碼内容。

我們的html代碼在現在需要添加name進去才能夠進行正常的擷取操作。

<body>
    <form method="POST" >
        <input type="text" id="name" name="name"/><hr>
        <button type="submit" >送出</button>
    </form>
</body>
           

修改後就可以直接運作啦

for屬性不适用,我嘗試過了把源代碼for改成name應該就可以了

繼續閱讀