Node.JS – Express请求在从表单提交POST请求时返回undefined。

huangapple go评论74阅读模式
英文:

Node.JS - Express request returns undefined when when submitting a post request from a form

问题

当我尝试使用Node.JS/Express获取HTML表单时,似乎会返回未定义的值,因为req.body返回一个空对象。

Node

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = '3000';

app.use(bodyParser.urlencoded({ extended: true }))

app.use(bodyParser.json());

app.use(express.static('public'))

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/signup.html')
});

app.post('/', function (req, res) {
    const FirstName = req.body.firstName
    const LastName = req.body.lastName

    console.log(FirstName)
    console.log(LastName)
});

app.listen(port, function (err) {
    if (err) console.log(err)
    console.log('Local port started at', port)
});

HTML

<form action="/" method="post">
  <label for="firstName">First Name</label>
  <input class="form-grid__item--field" type="text" id="firstName" placeholder="First Name">

  <label for="lastName">Last Name</label>
  <input class="form-grid__item--field" type="text" id="lastName" placeholder="Last Name">
              
  <label for="subscriberEmail">Email</label>
  <input class="form-grid__item--field" type="text" id="subscriberEmail" placeholder="Email">
          
  <input type="submit" class="form__button" value="Sign Me Up!">
</form>

多次重新格式化了代码。检查了HTML表单以确保actionmethod属性设置正确。查看了Body Parser的文档以尝试获得更多上下文,但没有发现问题。

英文:

When I try to get an HTML form from using Node.JS/Express it seems to return a undefined value as the req.body returns an empty object.

Node

const express = require(&#39;express&#39;)
const bodyParser = require(&#39;body-parser&#39;)
const app = express()
const port = &#39;3000&#39;


app.use(bodyParser.urlencoded({ extended: true}))


app.use(bodyParser.json());  

app.use(express.static(&#39;public&#39;))

app.get(&#39;/&#39;,function(req, res) {

    res.sendFile( __dirname + &#39;/signup.html&#39;)

});

app.post(&#39;/&#39;, function(req, res) {
const FirstName = req.body.firstName
const LastName = req.body.lastName

console.log(FirstName)
console.log(LastName)

});

app.listen(port, function(err) {
if (err) console.log(err)
console.log(&#39;Local port started at&#39;, port)
});

HTML

&lt;form action=&quot;/&quot; method=&quot;post&quot;\&gt;
  &lt;label for=&quot;firstName&quot;\&gt;First Name\&lt;/label\&gt;
  &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;firstName&quot; placeholder=&quot;First Name&quot;\&gt;

  &lt;label for=&quot;lastName&quot;&gt;Last Name&lt;/label&gt;
  &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;lastName&quot; placeholder=&quot;Last Name&quot;&gt;
              
  &lt;label for=&quot;subscriberEmail&quot;&gt;Email&lt;/label&gt;
  &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;subscriberEmail&quot; placeholder=&quot;Email&quot;&gt;
          
  &lt;input type=&quot;submit&quot; class=&quot;form__button&quot; value=&quot;Sign Me Up!&quot;&gt;
&lt;/form&gt;

Reformatted the code several times. Looked into the html form to ensure the action and methods where in place. When over body parser documentation to try and get more context but nothing.

答案1

得分: 0

 &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;subscriberEmail&quot; name=&quot;subscriberEmail&quot; placeholder=&quot;Email&quot;&gt;

“input”标签内的“id”字段用于在DOM中引用元素。

为了将输入作为请求中的字段传递,您必须给它添加属性“name”。

英文:

The id field inside input label is used for referencing the element in the DOM.

In order to pass the input as a field in the request you must give it attribute name

 &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;subscriberEmail&quot; name=&quot;subscriberEmail&quot; placeholder=&quot;Email&quot;&gt;

答案2

得分: 0

req.body 搜索表单中的 name 属性。添加 name 属性,然后通过 req.body.theNameOfTheAttribute 访问它们。

<form action="/" method="post">
  <label for="firstName">名字</label>
  <input class="form-grid__item--field" type="text" id="firstName" placeholder="名字">

  <label for="lastName">姓氏</label>
  <input class="form-grid__item--field" type="text" id="lastName" placeholder="姓氏">
              
  <label for="subscriberEmail">电子邮件</label>
  <input class="form-grid__item--field" type="text" id="subscriberEmail" placeholder="电子邮件">
          
  <input type="submit" class="form__button" value="注册">
</form>
英文:

The req.body searches for the name attribute in your form. Add the name attribute and then access them via req.body.theNameOfTheAttribute

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;form action=&quot;/&quot; method=&quot;post&quot;\&gt;
  &lt;label for=&quot;firstName&quot;\&gt;First Name\&lt;/label\&gt;
  &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;firstName&quot; placeholder=&quot;First Name&quot;\&gt;

  &lt;label for=&quot;lastName&quot;&gt;Last Name&lt;/label&gt;
  &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;lastName&quot; placeholder=&quot;Last Name&quot;&gt;
              
  &lt;label for=&quot;subscriberEmail&quot;&gt;Email&lt;/label&gt;
  &lt;input class=&quot;form-grid__item--field&quot; type=&quot;text&quot; id=&quot;subscriberEmail&quot; placeholder=&quot;Email&quot;&gt;
          
  &lt;input type=&quot;submit&quot; class=&quot;form__button&quot; value=&quot;Sign Me Up!&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月7日 04:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75035592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定