Can';即使设置了超时,net::ERR\u EMPTY\u RESPONSE error也不能使jQuery ajax调用持续超过4分钟
我有一个node.js服务器正在运行,我正在通过jquery对该服务器进行一系列ajax调用。其中一个ajax调用预计需要大约5分钟才能完成。我发现打电话时,它总是在正好4分钟的时候超时,错误是“net::ERR\u EMPTY\u RESPONSE”。我看到过如下问题:但无论我是否设置了timeout参数(在jquery中为0表示“无超时”,或者我将其设置为大于4分钟的值,例如300000,即5分钟),都会发生这种情况。它发生在4分钟的时间点,每一次 有人知道为什么会这样吗?ajax jquery调用有上限吗?不管设置了什么超时参数,调用所需的时间有多长 下面是进行ajax调用的代码: (此try块位于一个函数中,用于对特定URL进行ajax调用。Can';即使设置了超时,net::ERR\u EMPTY\u RESPONSE error也不能使jQuery ajax调用持续超过4分钟,jquery,node.js,ajax,timeout,Jquery,Node.js,Ajax,Timeout,我有一个node.js服务器正在运行,我正在通过jquery对该服务器进行一系列ajax调用。其中一个ajax调用预计需要大约5分钟才能完成。我发现打电话时,它总是在正好4分钟的时候超时,错误是“net::ERR\u EMPTY\u RESPONSE”。我看到过如下问题:但无论我是否设置了timeout参数(在jquery中为0表示“无超时”,或者我将其设置为大于4分钟的值,例如300000,即5分钟),都会发生这种情况。它发生在4分钟的时间点,每一次 有人知道为什么会这样吗?ajax jque
URL
、jsonToSend
和timeout
都是此函数的参数。(具体地说,timeout
是一个int,一个以毫秒为单位的数字,在调用超时之前等待)
//catch block,函数的其余部分。正如@MadWard所回答的那样-我希望在这里发布这篇文章,以防其他人偶然发现!正在运行的nodejs服务器(ajax正在向其发送ajax调用),似乎有4分钟的默认超时时间。因此,无论我为jquery指定了什么超时时间来进行调用,如果调用时间超过4分钟,服务器本身都会出错 因为这是我自己的服务器,所以我可以通过简单地增加nodejs服务器本身的“timeout”属性来解决这个问题
超时取决于服务器。如果您试图访问的服务器有超时,您无法真正绕过它。如果是您的服务器,如何增加超时将出现在您正在使用的任何web服务器(Express、Express+Nginx、Apache等)的文档中MadWard-谢谢!我没有意识到。这是一个简单的nodejs服务器,我会马上调查。奇怪的是,Node的默认Http超时在@MadWard-哦,伙计…你在这里节省了一天…将我的nodejs服务器的超时参数调整为600000(10分钟),绝对做到了。现在一切都很完美!非常感谢您…对于曾经点击它的任何人-我的nodejs服务器正在定义的位置:var server=app.listen(serverPort,function(){var host=server.address().address var port=server.address().port});server.timeout=600000;//修复了它不用担心,我不想让你在一个简单的修复上花上几个小时的时间!
try {
var ajaxCallConfig = {
method: action,
url: url,
data: jsonToSend,
contentType: "application/json",
success: function (ret) {
deferred.resolve(ret);
},
error: function (xhr, textStatus, errorThrown) {
// I ALWAYS end up in this block, with a net::ERR_EMPTY_RESPONSE being thrown, when I call this function to the long running api endpoint
console.log("Error on Ajax call to " + url + "; json " +
JSON.stringify(jsonToSend) +
"; text status: " + textStatus +
"; error thrown: " + JSON.stringify(xhr));
// check if it was a timeout
if (textStatus === 'timeout') {
// some error handling my server does specific to timeouts...
// left it out as it's not relevant...
// note : never hit this block, when the net::ERR_EMPTY_RESPONSE issue happens. (when i call the long-running api)
// but I hit this block if i give some dummy low value to the 'timeout' param to force a timeout.
}
else {
if (xhr.responseJSON) {
// under this case, server sent the response and set
// the status code
deferred.reject(xhr.responseJSON);
}
else {
// under this case, the error status is not set by
// server, it may due to other reasons
deferred.reject("Connection Error: " +
" Connection to server cannot be established");
}
}
}
};
if (typeof timeout !== 'undefined') {
console.log("set a timeout of " + timeout); // timeout is definitely being set, as I see this in console logs
ajaxCallConfig.timeout = timeout; // timeout attr does work , because when I try this with a very low number, say 300, it times out immediately
}
jQuery.ajax(ajaxCallConfig); // here we're making the ajax call
}
var server = app.listen(serverPort, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
server.timeout = 600000; // this was not set before. The default appeared to have been 4 minutes. Setting this fixed it! (NOTE this should be in milliseconds - so this is 10 minutes!)