【问题标题】:How to access query parameter sent via form submit如何访问通过表单提交发送的查询参数
【发布时间】:2021-12-05 10:56:25
【问题描述】:

我无法在方法中访问查询参数值。在浏览器中,我看到完整的网址,例如 http://localhost:3000/form?ssid=user&password=acder

但是当我尝试使用 req.url 或 req.originalUrl 访问它时,它只返回 /form 。

PS:我正在使用https://www.npmjs.com/package/react-native-http-bridge 在本机反应中运行此服务器,所以我在这里给出了一个类似的 express.js 示例。在 express js 中,我也无法访问参数,甚至无法通过帖子访问。请帮忙。

const express = require('express')
const app = express()
const port = 3000
/*
cannot use these for company reasons
//var bodyParser = require('body-parser')  
//app.use(express.json())
//app.use(express.urlencoded({extended: true}))
//var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
//var urlencodedParser = bodyParser.urlencoded({ extended: false })
*/
app.get('/form', (req, res) => {
    let data = `<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}

.form-inline {  
display: flex;
flex-flow: row wrap;
align-items: center;
}

.form-inline label {
margin: 5px 10px 5px 0;
}

.form-inline input {
vertical-align: middle;
margin: 5px 10px 5px 0;
padding: 10px;
background-color: #fff;
border: 1px solid #ddd;
}

.form-inline .sbt { 
padding: 10px 20px;
background-color: #002060;
border: 1px solid #ddd;
color: white;
cursor: pointer;
padding: 10px;
min-height: 50px;
}

.form-inline button:hover {
background-color: #002060;
}

@media (max-width: 800px) {
.form-inline input {
margin: 10px 0;
}

.form-inline {
flex-direction: column;
align-items: stretch;
}
}
</style>
    <script>
    function sendData() {

       //window.location.replace('http://www.w3schools.com:1234/ssid='+ssid.value+'pass='+pass.value);
    }

    </script>
    </head>
    <body>
      <h2><b>Login</b></h2>
      <form class="form-inline"  action="/form1" method="post">
      <label for="ssid">Wi-Fi Name (SSID): </label>
      <input type="text" id="ssid" name="ssid" value=""><br><br>
      <label for="password">Wi-Fi Password: </label>
      <input type="text" id="password" name="password" value=""><br><br>
      <input class="sbt" type="submit" value="Connect">
      </form>
    </body>
    </html>`;

    //Build our response object (you can specify status, mime_type (type), data, and response headers)
    let res1 = {};
    res1.status = "OK";
    res1.type = "text/html";
    res1.data = data;
    res.set('Content-Type', 'text/html');
    res.send(Buffer.from(data));


})

app.post("/form1",urlencodedParser, (req, res) => {
    var fullUrl = req.protocol + '://' + req.get('host') + String(req.originalUrl);
    console.log("originalUrl",String(req.url))
    console.log("inside form1",req.body.ssid);
    return {"req.body.":"ok"}
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

【问题讨论】:

    标签: javascript html react-native xmlhttprequest


    【解决方案1】:

    “window.location.search”将为您提供“?ssid=user&password=acder”的查询答案

    【讨论】:

    • 我试过这个我得到 ReferenceError: window is not defined
    • 试试 ${window.location.search} 或 {window.location.search}
    • 如果它仍然无法正常工作并上传错误图片
    • 它不起作用,因为在 Node 中不存在窗口。如果你会在上面看到我的代码,我想访问 /form1 端点中的数据。
    猜你喜欢
    • 2021-01-27
    • 2016-09-09
    • 1970-01-01
    • 2014-04-10
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多