当日期输入中的日期更改非常快时,就会发生此错误。如果我每2秒以上更改一次日期,效果很好。但是,当日期输入更改得非常快时,会出现以下错误。简而言之,只有在第一个请求之后很快发出下一个请求时,才会出现此错误。我花了几个小时在SO和谷歌上搜索了很多类似的错误ERR_CONNECTION_REFUSED,但运气不好。你知道是什么导致了这个问题吗?
此外,当加载url时(http://localhost:8000/svc/diary/2016-01-03)在浏览器中,数据显示得很好,但如果我很快重新加载(Ctrl+R)url,它会用以下术语进入雅虎搜索:
http localhost 8000 svc日记2016 01 03日记
来自控制台的错误消息:
GET http://localhost:8000/svc/diary/2016-01-03 net::ERR_CONNECTION_REFUSED
send @ jquery-2.2.0.js:9172
jQuery.extend.ajax @ jquery-2.2.0.js:8653
(anonymous function) @ idiary.js:18
jQuery.event.dispatch @ jquery-2.2.0.js:4732
elemData.handle @ jquery-2.2.0.js:4544
日期表单
<input class="form-control" id='ddate' type="date" name="bday">
Ajax调用
$('#ddate').on('change', function() {
var ajaxParams = {
url: '/svc/diary/' + $(this).val(),
type: 'GET',
contentType: "application/json",
dataType:"json"
};
console.log(ajaxParams);
$.ajax(ajaxParams)
.done(function (data, textStatus, jqXHR) {
console.log("diary retrieved!")
console.log(data);
$("#diary").text(data.diary);
})
.fail(function (jqXHR, textStatus, errorThrown) {
$("#diary").text("");
console.log("failed to retrieve diary!");
console.log(errorThrown);
});
});
后端node.js GET处理程序
function getDiary(req, res) {
var date = req.params.date;
util.log(mysql.format(searchQuery, [date]));
util.dbpool.query(searchQuery, [date], function (err, result) {
res.setHeader("Content-Type", "application/json");
if (err) {
console.log(err);
util.errLog(JSON.stringify(err));
res.status(500).json({message: 'summary, no results.'});
} else {
console.log(result);
result.length > 0 ? res.status(200).json(result[0]) : res.status(200).json({"ddate":date, "diary":""});
}
});
}
更新:刚刚发现,在使用启动节点应用程序服务器时,这个问题已经消失
node server.js
只有在使用pm2或foerver 启动节点应用程序服务器时才会出现此问题
pm2 start --watch server.js
forever start --watch server.js
问题已解决。它是由在pm2和forever中使用watch标志引起的。我的应用程序打印日志语句,日志文件夹与应用程序文件夹位于同一级别,因此每当更新日志文件时,pm2都会重新启动我的应用服务器。这就是为什么我每隔几秒钟就固定一次应用程序服务器,因为服务器重新启动只需要大约一秒钟。如果我很快锁定服务器,它会给出拒绝连接的错误,因为服务器仍在重新启动,这就是它被拒绝连接的原因。
您希望进行跨域请求。默认情况下,服务器不允许这样做。您需要在服务器上启用它,并发出JSONP请求。你可以在这里阅读:
http://www.leggetter.co.uk/2010/03/12/making-cross-domain-javascript-requests-using-xmlhttprequest-or-xdomainrequest.html
JSONP的全部内容是什么?