无法发布/在节点中/表达js,表格问题

时间:2019-09-16 22:36:31

标签: node.js express

我正在尝试从表单发送数据,并且必须使用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)
})

2 个答案:

答案 0 :(得分:1)

您必须使用req.query.keywork来代替req.body.keywork,因为您使用的是@IDIRSamir提到的GET。

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);
});