【发布时间】:2018-12-12 23:10:45
【问题描述】:
我是使用服务器进行 Web 开发的初学者(仅在 uni 上过一门课程)。我有一个与 GET 请求有关的奇怪问题,如果我刷新太多次,它就会停止发送。这是 npm start 工作时的输出:
GET / 304 0.350 ms - -
GET /stylesheets/styles.css 404 28.359 ms - 1132
GET /javascripts/scripts.js 304 0.244 ms - -
GET /favicon.ico 404 11.692 ms - 1132
GET /skaterSummary.json 304 51.770 ms - -
垃圾邮件刷新很多次后,输出变成这样:
GET /skaterSummary.json - - ms - -
GET / 304 0.676 ms - -
GET /stylesheets/styles.css 404 28.628 ms - 1132
GET /javascripts/scripts.js 304 0.599 ms - -
GET /favicon.ico 404 20.098 ms - 1132
代码:routes/index.js
// Database Testing
router.get('/skaterSummary.json', function(req, res, next) {
req.pool.getConnection(function(err, connection) {
if (err) throw err;
var query = "SELECT * from Summary;";
connection.query(query, function(err, results) {
res.json(results); // send response
});
});
});
代码:scripts.js
$(document).ready(function() {
'use strict';
let xhttp = new XMLHttpRequest();
xhttp.open('GET', 'skaterSummary.json', true);
xhttp.setRequestHeader('Content-type', 'application/json');
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("Success");
console.log(JSON.parse(xhttp.responseText));
}
};
});
感谢任何指点:)
【问题讨论】:
-
什么停止发送?
skaterSummary.json?你检查浏览器控制台了吗? -
是的,skaterSummary.json 停止发送。只有当它发送时,我才能在浏览器控制台中获得 json。
-
我会将
if (err) throw err;添加到您的connection.query回调中。 -
我认为这解决了它!工作了一段时间,但同样的事情又发生了:(
-
它不应该修复任何东西,它应该在发生错误时显示错误,而不是什么都不做。
标签: javascript node.js express web server