Javascript 将值从视图传递到Nodejs中没有URL的节点
我将nodejs与express和ejs一起使用 互联网上的每个人都会问如何将值从节点传递到视图,但反过来呢 例如,我要求我的用户在表单中输入一个字符串,当用户单击按钮时,我如何获得该字符串而不将其作为url中的参数传递 表格:Javascript 将值从视图传递到Nodejs中没有URL的节点,javascript,node.js,express,ejs,Javascript,Node.js,Express,Ejs,我将nodejs与express和ejs一起使用 互联网上的每个人都会问如何将值从节点传递到视图,但反过来呢 例如,我要求我的用户在表单中输入一个字符串,当用户单击按钮时,我如何获得该字符串而不将其作为url中的参数传递 表格: <div class="container"> <form style="width:50%"> <div class="form-group"> <lab
<div class="container">
<form style="width:50%">
<div class="form-group">
<label for="text">Name of the product</label>
<input type="text" class="form-control" id="pName">
</div>
<div class="form-group">
<label for="text">Reciever</label>
<input type="text" class="form-control" id="reciever">
</div>
<div class="form-group">
<label for="text">Location</label>
<input type="text" class="form-control" id="location">
</div>
<!-- <div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>-->
<button type="submit" class="btn btn-default">Submit</button>
</form>
我知道我能做到:
app.get('/url/:parameter', function(req.res) {
var foo = req.params.parameter;
}
但是如果我不想使用URL,是否可以检索数据?您可以使用POST方法而不是GET。你需要把快车的路线改成
app.post('/url', function(req.res))
并在窗体上添加一个方法
<form style="width:50%" method="POST">
使用POST作为html表单的一种方法
<form action="/myapi" method="post">
<input type="text" class="form-control" id="pName" name="pName">
<button type="submit" class="btn btn-default">Submit</button>
</form>
如果使用POST请求,则参数不是URL的一部分。例如
app.post('/path', function(req, res) {
...
//You can retrieve the parameters of the POST request here
]);
您需要主体解析器
模块才能获取POST参数。下面介绍如何在设置路线后获取POST参数
您的表单应该有一个方法和操作:
<form action="/path" method="POST">...</form>
。。。
根据您的问题,一般来说,如果您想从唯一id获取一个值,您将把该值存储为全局变量。因此,您可以轻松获得当前用户和用户相关的详细信息
app.post('/path', function(req, res) {
...
//You can retrieve the parameters of the POST request here
]);
<form action="/path" method="POST">...</form>