首页 > 解决方案 > 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();
      })
     })
    })

当我在查询中插入静态变量时,会显示数据,传递参数不会显示它。获取参数时可能是一个问题。

标签: mysqlnode.jsexpresspug

解决方案


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();
}

推荐阅读