node《获取表单中传递的数据》

2022-10-07 11:35:36
<form action="/btn" method="post">
		文本框:<input type="text" name="txt" value=""><br>
		密码框<input type="password" name="pass" value=""><br>
		多选:<input type="checkbox" name="vehicle" value="Bike">I have a bike
			  <input type="checkbox" name="vehicle" value="Car">I have a car<br>
		单选:<input type="radio" name="sex" value="male">男<br>
			  <input type="radio" name="sex" value="female">女<br>
		<button type="submit">提交</button>
	</form>

当有form标签时,浏览器就会提交表单中的数据,action是设置要提交的路径,因为是提交到本地服务器,所以路径只需要用/,method设置表单提交的方法,get或者post
表单中必须要有name和value属性,单选和多选设置的name相同即可
点击按钮提交后,服务器得到的数据是以name=value的形式,多选框是一个数组
在这里插入图片描述

在服务端接收post请求参数的方法:
//引入接收post请求参数模块
const bodyParser =require('body-parser');
//配置bodyParser
app.use(bodyParser.urlencoded({extended:false}));
app.post('/btn',(req,res)=>{
	console.log(req.body);})
  • 作者:st紫月
  • 原文链接:https://blog.csdn.net/ziyue13/article/details/115917531
    更新时间:2022-10-07 11:35:36