我正在尝试从表单发送数据,并且必须使用GET方法在路由上接收它。但是,它不起作用。我得到以下信息:
无法发布/仪表板
HTML
<form action="dashboard" method="get" class="uk-grid-small" uk-grid>
<div class="uk-width-1-4@s">
<input
class="uk-input"
type="text"
placeholder="Enter the keyword"
name="keyword"
/>
</div>
<div class="uk-width-1-4@s">
<select class="uk-select">
<option>Anywhere</option>
<option>España</option>
</select>
</div>
<div class="uk-width-1-4@s">
<select class="uk-select">
<option>Any language</option>
<option>Spanish</option>
</select>
</div>
<div class="uk-width-1-4@s">
<button class="uk-button uk-button-default">Search</button>
</div>
</form>
Node.js / Express
app.get('/dashboard', async (req, res) => {
var pal = req.body.keyword
console.log(req.body.keyword)
})
答案 0 :(得分:1)
app.get('/dashboard', async (req, res) => {
var pal = req.query.keyword;
console.log(req.query.keyword);
});
答案 1 :(得分:0)
您只是在action =“ / dashboard”中忘了斜杠,而使用req.query.keyword来获取数据
<form action="/dashboard" method="get" class="uk-grid-small" uk-grid>
<div class="uk-width-1-4@s">
<input class="uk-input" type="text" placeholder="Enter the keyword" name="keyword">
</div>
<div class="uk-width-1-4@s">
<select class="uk-select">
<option>Anywhere</option>
<option>España</option>
</select>
</div>
<div class="uk-width-1-4@s">
<select class="uk-select">
<option>Any language</option>
<option>Spanish</option>
</select>
</div>
<div class="uk-width-1-4@s">
<button class="uk-button uk-button-default">Search</button>
app.get('/dashboard', async (req, res) => {
var pal = req.query.keyword;
console.log(req.query.keyword);
});