使用 express js 和正文解析器从表单获取用户输入
getting user input from form using express js and body parser
我在从表单获取用户输入时遇到问题。
你能告诉我哪里错了吗??
这是添加-employee.html代码
<!DOCTYPE HTML>
<html>
<head>
<title>Add Employee</title>
<!--<script type="text/javascript" src="main.js"></script> -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<table>
<form action="/form" method="post">
<tr>
<th>
<label>Name:</label>
<input type="text" name="name">
</th>
<th>
<label>Designation:</label>
<input type="text" name="designation">
</th>
<th>
<label>PAN No:</label>
<input type="text" name="pan">
</th>
<th>
<label>Aadhar No:</label>
<input type="text" name="aadhar">
</th>
<th>
<label>Bank A/c:</label>
<input type="text" name="bank">
</th>
<th>
<label>Basic Salary:</label>
<input type="text" name="basicsalary">
</th>
<th>
<label>Other Allowance:</label>
<input type="text" name="allowance">
</th>
<th>
<label>ESI No:</label>
<input type="text" name="esi">
</th>
<th>
<label>UAN No:</label>
<input type="text" name="uan">
</th>
</tr>
<tr>
<td>
<input type="submit" name="submit_button">
</td>
</tr>
</form>
</table>
</body>
</html>
这是 link 到 server.js 代码,我正在 运行 应用程序中使用它。
https://github.com/silentarrowz/payroll/blob/master/server.js
当我提交表格时,我希望得到回复
'employee' + 姓名 + 'added'
但我得到的结果是“添加了未定义的员工”。
现在,为什么会这样?
我哪里错了?
在你的 server.js 中你有这条线。
app.use(bodyParser.json());
这意味着您的服务器只能读取请求中的 JSON 数据。但是,除非您使用 javascript/jquery 将表单发送到服务器,否则数据永远不会被格式化为 JSON。相反,浏览器使用 urlencoded 或多部分格式来发送表单。 multipart 通常仅在您使用表单发送文件时使用,因此很可能使用 urlencoded 格式。
这意味着您需要为 urlencoded 格式包含一个正文解析器。
只需更改它,这样我们在 json 解析器下面就有了 urlencoded 解析器,如下所示:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
但是,如果您将使用表单发送文件,则必须使用 multiparty.
等模块解析请求
我在从表单获取用户输入时遇到问题。 你能告诉我哪里错了吗??
这是添加-employee.html代码
<!DOCTYPE HTML>
<html>
<head>
<title>Add Employee</title>
<!--<script type="text/javascript" src="main.js"></script> -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<table>
<form action="/form" method="post">
<tr>
<th>
<label>Name:</label>
<input type="text" name="name">
</th>
<th>
<label>Designation:</label>
<input type="text" name="designation">
</th>
<th>
<label>PAN No:</label>
<input type="text" name="pan">
</th>
<th>
<label>Aadhar No:</label>
<input type="text" name="aadhar">
</th>
<th>
<label>Bank A/c:</label>
<input type="text" name="bank">
</th>
<th>
<label>Basic Salary:</label>
<input type="text" name="basicsalary">
</th>
<th>
<label>Other Allowance:</label>
<input type="text" name="allowance">
</th>
<th>
<label>ESI No:</label>
<input type="text" name="esi">
</th>
<th>
<label>UAN No:</label>
<input type="text" name="uan">
</th>
</tr>
<tr>
<td>
<input type="submit" name="submit_button">
</td>
</tr>
</form>
</table>
</body>
</html>
这是 link 到 server.js 代码,我正在 运行 应用程序中使用它。 https://github.com/silentarrowz/payroll/blob/master/server.js 当我提交表格时,我希望得到回复 'employee' + 姓名 + 'added'
但我得到的结果是“添加了未定义的员工”。
现在,为什么会这样? 我哪里错了?
在你的 server.js 中你有这条线。
app.use(bodyParser.json());
这意味着您的服务器只能读取请求中的 JSON 数据。但是,除非您使用 javascript/jquery 将表单发送到服务器,否则数据永远不会被格式化为 JSON。相反,浏览器使用 urlencoded 或多部分格式来发送表单。 multipart 通常仅在您使用表单发送文件时使用,因此很可能使用 urlencoded 格式。
这意味着您需要为 urlencoded 格式包含一个正文解析器。
只需更改它,这样我们在 json 解析器下面就有了 urlencoded 解析器,如下所示:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
但是,如果您将使用表单发送文件,则必须使用 multiparty.
等模块解析请求