Javascript 调用app.use/app.post(插入数据库)获取表单值
我创建了一个app.use with express,以便在数据库中插入值。我想在用户单击register时获取这些值 我的表格:Javascript 调用app.use/app.post(插入数据库)获取表单值,javascript,jquery,node.js,express,Javascript,Jquery,Node.js,Express,我创建了一个app.use with express,以便在数据库中插入值。我想在用户单击register时获取这些值 我的表格: <form class="form-signin" method="POST"> <h1 class="h3 mb-3 font-weight-normal">Register</h1> <label for="inputEmail" id="lblEmail" class="sr-only">Email addres
<form class="form-signin" method="POST">
<h1 class="h3 mb-3 font-weight-normal">Register</h1>
<label for="inputEmail" id="lblEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email
address" name="email" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control"
placeholder="Password" name="passworf" required>
<label for="repeatInputPassword" class="sr-only">Repeat Password</label>
<input type="password" id="repeatinputPassword" class="form-control"
placeholder="Repeat Password" required>
<label for="name" class="sr-only">Name</label>
<input type="name" id="inputName" class="form-control" placeholder="Name"
name="name" required>
<label for="surname" class="sr-only">Surname</label>
<input type="surname" id="inputSurname" class="form-control"
placeholder="Surname" name="surname" required>
<div class="checkbox mb-3">
</div>
<button class="btn btn-lg btn-primary btnblock-"
type="submit">Register</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
您的表单必须将
POST
请求发送到使用app定义的同一路线。请使用(…)
。在这种情况下,您的表单是POST
ing到它所在的路径,这意味着它需要在/
路径上才能工作
将您的应用程序更改为app.post(…)
,使用(…)
使其仅处理post
请求,然后使用以下方法从同一路径提供表单的html:
app.get('/', function(req, res) {
res.sendFile(path-to-form);
})
请参阅:您需要创建一个表单,该表单也可以发布
app.post('/', function (req, res) {
// Process form here
var adminUser = req.body.email;
var adminPass = req.body.password;
var adminName = req.body.name;
var adminSurname = req.body.surname;
// etc.
});
路径应与表单属性匹配您是在询问如何提交html表单吗?然后,是的,您是在询问如何提交html表单。对于初学者,您的表单缺少开始标记。如果您的输入没有名称,它的值将不会发送到服务器。不,我的意思是您的输入需要名称。
app.post('/', function (req, res) {
// Process form here
var adminUser = req.body.email;
var adminPass = req.body.password;
var adminName = req.body.name;
var adminSurname = req.body.surname;
// etc.
});