簡體   English   中英

GET表單和Node.JS和Express的問題

[英]Issue with GET forms and Node.JS and Express

我用HTML編寫了一個非常簡單的表單,它使用Express將GET信息發送到我的Node.JS服務器。 這是形式:

    <form method="get" action="/search" autocomplete="off" class="navbar-search pull-left">
       <input name="search" type="text" id="search" data-provide="typeahead" placeholder="Search..." />
    </form>

這是服務器部分:

app.get('/search', function (req, res){

   console.log(req.query["search"]);

   res.render('search.ejs')

});

當我在輸入中寫入內容並按回車鍵時,頁面會長時間加載,當我進入http://localhost:8080/search?search=foo時,我收到340錯誤。 我認為我有一個問題,從而沒有正確發送值,因為它也不適用於POST請求。 對此有何解決方案?

謝謝你的進步!

這是因為你必須使用req.params.search而不是req.query,它不起作用。

app.get('/search', function (req, res){
   var search = req.query.search;

   console.log(search);

   res.render('search.ejs')

});

在這里,您可以閱讀更多相關信息: http//expressjs.com/api.html#req.param

下次找出一個erorr,輸入app.get(... console.log(res, req);並找到你的變量所在的位置。

非常有趣,對我來說它有用!

app.get('/search', function (req, res){
   var search = req.query;

   console.log(search, 'of type', typeof search);
   res.send("the query is about the student with name " + search.sname + ' and subject '+search.ssubject );

});

與形式

<form method= 'GET' action="/search" autocomplete="off">
            Student Name:<br>
            <input type="text" name="sname">
            <br>
            Student Subject:<br>
            <input type="text" name="ssubject">
            <br>
            <input type="submit" value="Submit">
            <input type="reset" value="Reset">
        </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM