【发布时间】:2018-05-06 16:23:33
【问题描述】:
我正在尝试从 MySQL 中提取数据并将其显示在我的 HTML 页面上,但是当我在浏览器 http://localhost:3000 上运行以下代码时,数据不会显示在我的页面上。如果有人能帮我解决这个问题,我将不胜感激。
index.html
<!DOCTYPE>
<html>
<head>
<title>Data from MySQL</title>
</head>
<body>
<div id="output_message"></div>
</body>
</html>
app.js
var express = require('express');
var app = express();
var mysql = require('mysql');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', express.static(__dirname + '/'));
app.set('view engine', 'html');
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mywebsite"
});
connection.connect();
app.get('/',(req, res) => {
connection.query("SELECT * FROM chat",(err, result) => {
if(err) {
console.log(err);
res.json({"error":true});
}
else {
console.log(result);
res.json(result);
}
});
});
app.listen(3000, function () {
console.log('Connected to port 3000');
});
【问题讨论】:
-
如果您在控制台中遇到任何错误,您能告诉我们吗?
-
控制台没有错误
-
嗯,原因是你通过GET方法发送一个空的html文件
-
如何将其发送到我的 html 文件?
-
在GET方法中包含POST方法逻辑,并去掉post