我正在嘗試從 HTML 表單中讀取輸入并在 Node.js 中使用它。
這是我的 HTML 表單:
<form action="/myform" method="POST">
<input type="text" name="mytext" required />
<input type="submit" value="Submit" />
</form>
這是我的 Node.js 代碼:
app.post('/myform', function(req, res){
console.log(req.body.mytext); //mytext is the name of your input box
});
我收到此錯誤:
TypeError: Cannot read properties of undefined (reading 'mytext')
Please help me out.
uj5u.com熱心網友回復:
確保您正在決議請求。為此,在服務器根檔案的頂部添加以下行:
app.use(express.urlencoded({
extended: true
}));
uj5u.com熱心網友回復:
您必須使用內置方法 express.json() 來決議 json 資料。要使用此方法,您需要中間件 app.use()
所以你必須像下面一樣定義它,但在主檔案中定義任何路由之前。
app.use(express.json())
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/437513.html
標籤:javascript html 节点.js 表示
