我正在尝试使用前端的jquery显示从数据库(sql server)中提取的表。但它继续显示JSON格式。下面是我到目前为止尝试过的代码。 此外,当我执行它时,它会打开一个下载框并下载json文件。它应该只是打开浏览器并显示数据表。
server.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
user: 'clientinfo',
password: 'ClientInfo123',
server: 'USD3342',
database: 'Client'
};
sql.close();
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from dbo.Logs', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(1337, function () {
console.log('Server is running..');
});
的index.html
<html>
<head>
<title>Error Log</title>
<script src="/server.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script
src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js">
</script>
<link rel="stylesheet"
href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" />
<script>
$(document).ready(function () {
$('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "server.js/recordset",
"method": "post",
"dataType": "json",
"success": function (data) {
$('#example').DataTable({
recordset: data,
columns: [
{ 'data': 'Message' },
{ 'data': 'Userid' },
{ 'data': 'DateUTC' },
{ 'data': 'Environment' },
{ 'data': 'Query' },
{ 'data': 'ShadowId' },
{ 'data': 'StackTrace' }
]
});
}
}
});
});
</script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100">
<thead>
<tr>
<th>Message</th>
<th>Userid</th>
<th>DateUTC</th>
<th>Environment</th>
<th>Query</th>
<th>ShadowId</th>
<th>StackTrace</th>
</tr>
</thead>
</table>
</body>
</html>
package.json (以防万一)
{
"author": "",
"dependencies": {
"express": "^4.15.3",
"mysql": "2.13.0",
"tedious": "2.0.0",
"mssql": "4.0.4"
},
"description": "",
"devDependencies": {},
"license": "ISC",
"main": "index.html",
"name": "nodejs-web-app1",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "1.0.0"
}