带有nodejs和mysql的jquery datatable

gt0wga4j  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(476)

我想显示数据表中的值。我打开chrome开发工具,进入network标签页xhr,问题是,响应是一个html文档
服务器端代码:

router.get('/agents', (req, res) => {
  let dB = req.dB;
  let sql = 'SELECT * FROM tbl_users WHERE role_id = 3';

  dB.query(sql, (err, agents) => {
    if (err) throw err;
    const data = { agents };
    console.log(data);

    res.render('dashboard/agents', data);
  });
});

客户端脚本:

$('#agent_table').DataTable( {
  processing: true,
  serverSide: true,
  ajax: {
    url: '/administrator/agents',
    type: 'GET',
    dataSrc: 'agents'
  },
  columns: [
    { data: 'user_id' },
    { data: 'checked' },
    { data: 'first_name' },
    { data: 'middle_name' },
    { data: 'role_id' },
  ]
} );

客户端htmlt:

<table id="agent_table" class="table table-striped table-bordered dt-responsive nowrap"
            cellspacing="0" width="100%">
            <thead>
              <tr>
                <th>User ID</th>
                <th>
                  <div class="checkbox checkbox-success select_all">
                    <input class="styled" type="checkbox" id="check_all">
                    <label for="check_all" class="check_all_label">All</label>
                  </div>
                </th>
                <th>First Name</th>
                <th>Middle Name</th>
                <th>Last Name</th>
                <th>Role</th>
              </tr>
            </thead>
          </table>

控制台登录服务器
{代理:[rowdatapacket{user\u id:6,名字:'pamela',中间名:'pearl',姓氏:'sabes',电子邮件:'test@gmail.com',密码:'1bd07c9db7ae63c02f2ee75471727f58',手机号码:'09503713607',邀请代码:'',生日:'2005年11月29日',性别:'female',地址:'',图片:null,联系人:'',联系人:'',联系人编号:'',角色id:3,经度:'',纬度:'',医疗信息:'',已验证:1,已删除:0,创建时间:''0000-00-00 00:00:00',更新时间:''2018-05-30t20:42:43.000z}]}

ctrmrzij

ctrmrzij1#

我认为问题在于你对res.render的使用。因为根据express文档res.render()函数编译模板,在模板中插入局部变量,并用这两个元素创建html输出并发送。所以试试这个。 res.json(data)

r6l8ljro

r6l8ljro2#

var url=req.protocol+'://'+req.get('主机');

var draw = req.body.draw;
var row = req.body.start;
var count = 1+parseInt(row);
var rowperpage = req.body.length; // Rows display per page
//var columnIndex = req.query.order[0]['column']; // Column index
//console.log(req.body.search);
var dataArr = [];

adminTagModel.CountTagsAll(dataArr, function (totalRecordsValid)
{
    var tagDataobj = {
        row : row,
        rowperpage : rowperpage
    };
    adminTagModel.showTagsAll(tagDataobj, function (totalValid)
    {
        for (var i = 0; i < totalValid.length; i++) 
        {
            dataArr.push({  'count' : count++ ,'tag_name' : totalValid[i].tag_name,'action' : "<a href='"
                +url+"/admin/tagdetails/"+totalValid[i].t_id+"' target='_blank'  ><i class='fa fa-eye'  alt='View Detail'></i></a>"});
        }

        var response = {
            "draw" : draw,
            "iTotalRecords" : totalRecordsValid[0].allcount,
            "iTotalDisplayRecords" : totalRecordsValid[0].allcount,
            "aaData" : dataArr
        };
        res.send(response);
        return response;
    });

相关问题