[英]net::ERR_CONNECTION_REFUSED Error jQuery ajax node.js
當日期輸入中的日期更改很快發生時,會發生此錯誤。 如果我每2秒更改一次日期,它就可以正常工作。 但是當日期輸入變化非常快時,它會出現以下錯誤。 簡而言之,只有在第一個請求很快就在第一個請求之后發生時,才會發生此錯誤。 我已經花了幾個小時在SO和谷歌上搜索了這個錯誤ERR_CONNECTION_REFUSED的很多類似的問題,但沒有運氣。 是什么導致這個問題的想法?
此外,當在瀏覽器中加載URL( http:// localhost:8000 / svc / diary / 2016-01-03 )時,數據會很好,但如果我快速重新加載(Ctrl + R)網址,它會更快使用以下條款進行雅虎搜索:
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 Call
$('#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和永遠使用watch標志引起的。 我的應用程序打印日志語句,日志文件夾與應用程序文件夾處於同一級別,因此每當更新日志文件時,pm2都會重新啟動我的應用程序服務器。 這就是為什么如果我每隔幾秒鍾固定一次我的應用服務器就可以工作,因為服務器重啟只需要大約一秒鍾。 如果我非常快地固定服務器,它會給出連接拒絕錯誤,因為服務器仍在重啟,這就是拒絕連接的原因。
您正在尋求跨域請求。 默認情況下,服務器不允許這樣做。 您需要在服務器上啟用它,並發出JSONP請求。 你可以在這里讀到它:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.