[英]Undefined while console.log()
app.js 中的代碼
app.post('/compose', function(req, res){
const post = {
title: req.body.postTitle,
content: req.body.postBody
};
console.log(post);
res.redirect('/compose');
});
ejs 文件中的代碼
<%- include('partials/header') -%>
<h1>Compose</h1>
<form action="/compose" method="post">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control name="postTitle">
<label>Post</label>
<textarea class="form-control name="postBody" rows="8" cols="80"></textarea>
</div>
<button class="btn btn-primary" type="submit" name="submit">Publish</button
</form>
<%- include('partials/footer') -%>
輸出:輸出未定義
<button class="btn btn-primary" type="submit" name="submit">Publish</button>
而不是這個,請嘗試使用下面的代碼
<input type="submit" class="btn btn-primary" name="submit"/>
req 中的空體總是因為缺少bodyparser
。
請添加app.use(express.urlencoded());
在 app 聲明之后啟用解析你的身體。
PS:您不能提供 HTML 按鈕類型的提交。 您可以使用輸入類型提交或表單末尾的簡單按鈕來觸發提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.