我想在沒有任何框架(沒有快遞)的情況下處理來自 node.js 表單的資料,我看到了這樣的解決方案:
if (req.url === "/Login.html" && req.method == 'POST') {
req.on('data', (chunk) => {
console.log(chunk); //never gets fired
}).on('end', () => {
console.log("end"); //this does
});
{
res.writeHead(200, { 'Content-Type': 'text/html' })
fs.readFile("Login.html", (err, data) => {
res.write(data);
res.end();
});
}
}
有 Login.html 像:
<!DOCTYPE html>
<html>
<head>
<title>ESREP</title>
</head>
<body>
<div class="nav">
<a href="Produse.html">Produse</a>
<a href="Documentation.html">About</a>
<a href="Login.html">Login</a>
</div>
<div class="login">
<h1>Login</h1>
<form method="POST">
<div class="field">
<input type="email" placeholder="Email" required>
</div>
<div class="field">
<input type="password" placeholder="Password" required>
</div>
<button type="submit">Submit</button>
</form>
<div id="sign">
<p>Sign up <a href="Signup.html">here</a></p>
</div>
</div>
</body>
</html>
但似乎 req.on('data',()) 部分永遠不會被訪問。req.on('end') 總是這樣。
uj5u.com熱心網友回復:
您的表單沒有成功的欄位(name屬性是成功的先決條件)因此沒有資料,因此發送內容長度為零的正文(即沒有資料)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/479682.html
