前台提交数据到node服务器(get方式)
2024-08-29 20:07:16
、有两种办法,一种是表单提交,一种是ajax方式提交。
1、form提交
在前台模板文件上写:
<form action="/reg" method="get">
<input type="text" />
<input type="password" />
<input type="submit" />
</form
index.js添加一个路由规则:
router.get('/reg',(req,res) => {
//req,浏览器发送给服务器的数据保存在这里
//参数1:数据库代码 参数2:动态值 参数3:回调
sql('insert into `user` (`id`,`username`,`password`) values (0,?,?)',[req.query.name,req.query.password],(err,result) => {
if(err){
console.log('[INSERT ERROR] - ',err.message);
return;
}
res.json({
success : '[INSERT SUCCESS] - '
});
});
});
2、ajax提交
(1)先引入jQuery;
(2)前台模板文件写:
<input type="text" name="name" class="name" />
<input type="password" name="password" class="psw" />
<input type="submit" class="submit" />
<script src="/jquery.js"></script>
<script>
$('.submit').click(function () {
$.ajax({
url : '/reg',
type : 'get',
data : {
name : $('.name').val(),
password : $('.psw').val()
},
success : function (data) {
console.log(data);
}
});
});
</script>
最新文章
- asp.net timer viewstate
- 修改BIND9实现TCP DNS
- Android 项目实战--手机卫士(实现splash)
- 【leetcode】Substring with Concatenation of All Words (hard) ★
- 由case 和 break 引发的思考
- OpenJudge计算概论-找最大数序列
- java集合 collection-list-LinkedList
- 谋哥:App排行榜的秘密
- java笔记8之选择结构IF
- 帝国cms7.0调用指定栏目,指定顺序排列
- Oracle行转列实例
- How to find variable is empty in shell script
- Selenium也是一个用于Web应用程序测试的工具
- css hack总结
- C#杂记-简化的初始化
- vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2) 但是 2:7.3.429-2ubuntu2.1 正要被安装
- Codeforces | CF1000B 【Light It Up】
- ReentrantLock可重入锁的理解和源码简单分析
- 7.Python 正则表达式学习笔记
- 【IT笔试面试题整理】不用加减乘除做加法
热门文章
- Web前端和Web后端的区分
- MYSQL批量导入数据报:[Err] 2006 - MySQL server has gone away 解决方法
- 怎样才算精通Linux
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
- 18_init 函数的使用
- DEVOPS基础
- SDU暑期集训排位(2)
- codeforces 701 E. Connecting Universities(树+ 边的贡献)
- codeforces 486 E. LIS of Sequence(dp)
- 题解 UVA12716 GCD等于XOR GCD XOR