net :: ERR_CONNECTION_REFUSED错误jQuery ajax node.js [英] net::ERR_CONNECTION_REFUSED Error jQuery ajax node.js

查看:1091
本文介绍了net :: ERR_CONNECTION_REFUSED错误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 
发送@ jquery-2.2.0.js:9172
jQuery.extend.ajax @ jquery-2.2.0。 js:8653
(匿名函数)@ idiary.js:18
jQuery.event.dispatch @ jquery-2.2.0.js:4732
elemData.handle @ jquery-2.2.0。 js:4544

日期表格

 < input class =form-controlid ='ddate'type =datename =bday> 




Ajax调用(''ddate')。on('change',function(){
var ajaxParams = {

 
url:'/ svc / diary /'+ $(this).val(),
类型:'GET',
contentType:application / json,
dataType :json
};
console.log(ajaxParams);
$ .ajax(ajaxParams)
.done(function(data,textStatus,jqXHR){$ b $ (数据);
$(#diary)。text(data.diary);

} )
.fail(函数(jqXHR,textStatus,errorThrown){
$(#diary)。text();
console.log(未能检索日记! );
console.log(errorThrown);
});
});




后端节点.js GET处理程序

 函数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结果。'});
} else {
console.log(result);
result.length> 0?res.status(200).json(result [0]): res.status(200).json({ddate:date,diary:});
}
});
}



UPDATE:刚刚发现,当启动节点应用程序服务器时,这个问题已经消失了

  node server.js 

仅当使用pm2或foerver启动节点应用程序服务器时才会发生此问题

  pm2 start --watch server.js 
永久启动--watch server.js


解决方案

问题已解决。这是由于在pm2中使用手表标志而导致的。我的应用程序打印日志语句,并且日志文件夹与应用程序文件夹处于同一级别,因此无论何时更新日志文件,pm2都将重新启动我的应用程序服务器。这就是为什么如果我每几秒钟连接我的应用程序服务器就会工作,因为服务器重启只需要一秒左右的时间。如果我非常快速地连接服务器,它会给连接拒绝错误,因为服务器仍在重新启动,这就是连接被拒绝的原因。


This error happens when the date change in the date input happens very quickly. If I change the date every 2+ seconds, it works fine. But it gives the following error when the date input is changed very fast. In short, this error happens only when the next request is made right after the first one very quickly. I've spent hours and search a lot of similar issue with this error ERR_CONNECTION_REFUSED on SO and google, but no luck. Any ideas of what's causing this issue?

Also, when load the url (http://localhost:8000/svc/diary/2016-01-03) in browser, the data comes out fine, but if I reload (Ctrl + R) the url very fast, it goes to yahoo search with these terms:

http localhost 8000 svc diary 2016 01 03 diary

The Error Message from the console:

    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

The Date form

<input class="form-control" id='ddate' type="date" name="bday">


The 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);
        });
});


The backend node.js GET handler

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":""});
        }
    });
}


UPDATE: Just found out, this issue is gone when starting the node application server with

node server.js

This issue happens only when starting the node application server with pm2 or foerver

pm2 start --watch server.js
forever start --watch server.js

解决方案

The issue is resolved. It was caused by using the watch flag in pm2 and forever. My app prints log statements, and the log folder is in the same level as the application folder, so whenever a log file is updated, pm2 is restarting my application server. That is why it works if I pin my application server every few seconds because the server restart only takes about a second. It gives connection refused error if I pin the server very fast, because the server is still restarting, that's why it's connection refused.

这篇关于net :: ERR_CONNECTION_REFUSED错误jQuery ajax node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆