简体   繁体   English

从 html 传递变量提交到 nodejs 服务器端

[英]Passing variable from html submit to nodejs server side

I am now practicing to make my own local server using with NodeJS and html.我现在正在练习使用 NodeJS 和 html 制作自己的本地服务器。

I would like to take some inputs from the front side using web and pass all the variables to NodeJS server side then running a python program to store those variables into my db.我想使用 web 从前端获取一些输入并将所有变量传递到 NodeJS 服务器端,然后运行 python 程序将这些变量存储到我的数据库中。 I am using POST method since I am storing some values into db.(Please correct me if I am doing wrong way.)我正在使用 POST 方法,因为我将一些值存储到数据库中。(如果我做错了,请纠正我。)

I am trying to take input from a user on front side using input tag in data.html.我正在尝试使用 data.html 中的输入标签从前端的用户那里获取输入。 Below is my part of what I am trying to do.以下是我正在尝试做的部分。 http://localhost:8080/data is my url address and I am taking two values which are called 'name' for the first and 'ssn' for the second. http://localhost:8080/data 是我的 url 地址,我取了两个值,第一个称为“名称”,第二个称为“ssn”。

<form action="http://localhost:8080/data/name/:name/ssn/:ssn" method="post" id="signup">
    <h1>Personal info post example</h1>
    <div class="field">
        <label for="name">Name:</label>
        <input type="text" id="name" placeholder="Enter your fullname" />
        <small></small>
    </div>
    <div class="field">
        <label for="ssn">SSN:</label>
        <input type="text" id="ssn" placeholder="Enter your SSN" />
        <small></small>
    </div>
    <button type="submit">Submit</button><br><br>
</form>

Below is my part of server.js.下面是我的 server.js 部分。 I tried to print out everything that I can think of what I can.我试图打印出我能想到的一切。 I wrote down the results as comments as well.我也把结果写成评论。 This is the result when I enter 'a' for the name and 'b' for the ssn and submit it.这是我输入“a”作为名称并输入“b”作为 ssn 并提交时的结果。

app.post('/data/name/:name/ssn/:ssn', function(req, res) {
    console.log('req.query: ', req.query); // {}
    console.log('req.query.name: ',req.query.name); // undefined
    console.log('req.query.ssn: ',req.query.ssn); // undefined
    console.log('req.params: ',req.params); // { name: ':name', ssn: ':ssn' }
    console.log('req.params.name: ',req.params.name); // :name
    console.log('req.params.ssn: ',req.params.ssn); // :ssn
});

When I type 'a' and 'b' into search boxes and hit the submit button, the web browser starting to load but never ends and my VSC shows the result.当我在搜索框中输入“a”和“b”并点击提交按钮时,web 浏览器开始加载但永远不会结束,我的 VSC 会显示结果。 Can anyone help me what I need to fix?谁能帮我解决我需要解决的问题?

your code is good.你的代码很好。 But you are facing this problem because the server is not sending any data after submit.但是您正面临这个问题,因为服务器在提交后没有发送任何数据。

First, there should be something to handle GET:首先,应该有一些东西可以处理 GET:

var express = require('express');
var app = express();

app.get('/', (req, res)=>{
    res.sendFile('index.html');
    console.log('Someone saw your website!');
})

app.listen(80);

Secondly, return something after submit:其次,提交后返回一些东西:

app.post('/data/name/:name/ssn/:ssn', function(req, res) {
    console.log('req.query: ', req.query); // {}
    console.log('req.query.name: ',req.query.name); // undefined
    console.log('req.query.ssn: ',req.query.ssn); // undefined
    console.log('req.params: ',req.params); // { name: ':name', ssn: ':ssn' }
    console.log('req.params.name: ',req.params.name); // :name
    console.log('req.params.ssn: ',req.params.ssn); // :ssn
    res.end('Thankyou!');
});

looks like you're mixing req.params and req.query , while you actually need req.body看起来你正在混合req.paramsreq.query ,而你实际上需要req.body

try this:尝试这个:

add this to server.js (you need to install body-parser):将此添加到 server.js(您需要安装 body-parser):

const bodyParser = require('body-parser');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// parse application/json
app.use(bodyParser.json());

app.post('/data', function(req, res) {
    console.log('req.body: ', req.body);

    const {name, ssn} = req.body;
    console.log('name: ', name);
    console.log('ssn: ', ssn);
    res.json(req.body);
});

and change HTML form (add name attribute, that's your data):并更改 HTML 表单(添加name属性,这是您的数据):

<form action="http://localhost:8080/data" method="post" id="signup">
    <h1>Personal info post example</h1>
    <div class="field">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" placeholder="Enter your fullname" />
        <small></small>
    </div>
    <div class="field">
        <label for="ssn">SSN:</label>
        <input type="text" id="ssn" name="ssn" placeholder="Enter your SSN" />
        <small></small>
    </div>
    <button type="submit">Submit</button><br><br>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM