mysql - nodejs,expressjs和jade中没有获取参数
问题描述
我有一个带有两个选择菜单的页面和一个显示数据的表格。我从 MySQL 中使用“get”获取数据并填充了选择菜单,现在当我单击“提交”按钮时,它应该在下表中显示数据而不刷新页面。我是新来的。
应用程序.js
app.get('/home', function(req, res){
db.connect(function(err){
var sale = req.query.cbosale;
var company = req.query.cbocompany;
db.query("SELECT DISTINCT(SaleNo) FROM tsales; SELECT DISTINCT(Company) FROM tcompany; SELECT * FROM trecords WHERE SaleNo = '"+sale+"' AND Company = '"+company+"'", [1,2,3], function(err, result, fields){
res.render('home', {title:"Home",data:result});
})
})
})
主页.jade
script.
$('#submit').click(function(){
var cbosale = $('#cbosale').val();
var cbocompany = $('#cbocompany').val();
$.get("/home", {cbosale: cbosale, cbocompany: cbocompany), function(data){
$('#showdata').show();
})
})
})
当我在查询中插入静态变量时,会显示数据,传递参数不会显示它。获取参数时可能是一个问题。
解决方案
There are two issues with your code i've spotted immediately.
The most important - not on topic, but it's important - your server is vulnerable to SQL injection - this is major security flaw and you should fix it. See for example: Preventing SQL injection in Node.js
Regarding your main question. You're not consuming result of request at all.
The data
of your callback is not used at all. Your $.get
callback should look similar to this:
function(data){
$('#dataTitle').val(data.title);
$('#dataResult').val(data.result); // assuming result is plain string or number
$('#showdata').show();
}
推荐阅读
- html - 我还需要在 2021 年添加 q 和 blockquote CSS 重置吗?拥有这个的目的是什么?
- python - 如何消除输入和标点符号之间的空格?
- docker - docker ps 显示容器已启动。但 docker exec 显示它没有运行
- javascript - 仅使用 Paypal 客户端集成是否安全?
- android - 索尼 bravia tv 中 Surfaceview 高于另一个 Surfaceview 交换时的问题
- c# - 将 Angular 11 集成到现有的 .Net Core 3.1 MVC 应用程序
- ajax - wordpress ajax 发布到数据库
- javascript - 我可以使用什么 JavaScript 方法来查找不同屏幕上页面的高度
- jython - SikuliX 无法调用 makeOpts()
- ckeditor - CKEditor:粘贴后如何保持样式/格式