我想显示数据表中的值。我打开 chrome 开发工具并转到网络选项卡、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>
控制台登录服务器
{ agents: [ RowDataPacket { user_id: 6, first_name: 'Pamela', middle_name: 'Pearl', last_name: 'Sabes', email: 'test@gmail.com', password: '1bd07c9db7ae63c02f2ee75471727f58', mobile_number: '09503713607', invite_code: '', birthday: 'Nov 29, 2005', gender: 'Female', address: '', image: null, contact_person: '', contact_person_number: '', role_id: 3, longitude: '', latitude: '', medical_info: '', is_verified: 1, is_deleted: 0, created_at: '0000-00-00 00:00:00', updated_at: 2018-05-30T20:42:43.000Z } ] }
最佳答案
我认为问题是你对 res.render 的使用。因为根据明确的文档 res.render() 函数会编译您的模板,在其中插入本地变量,并从这两个内容中创建 html 输出并将其发送。所以试试这个。
res.json(数据)
关于mysql - 使用 NodeJS 和 MySql 的 jQuery 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731153/