我有一个Node.js的服务器上运行,而我做了一系列的AJAX调用通过jQuery该服务器。一个Ajax调用,预计花费大约5分钟才能完成。我发现,在致电时,它总是超时,正好4分钟,用错误的净:: ERR_EMPTY_RESPONSE“。我所看到的问题,比如下面:ajax net::ERR_EMPTY_RESPONSE after waiting for response for 2 mins - node.js server但如果我设置的超时参数(无论是作为0,这意味着在jQuery的“无超时”,或者,如果我把它看成是4分多钟更大发生这种情况无论 - 例如300000,其为5分钟)。它发生在上DOT,每一次4分钟。
有没有人有会出现这种情况的任何想法?是否有jQuery的AJAX调用的上限,能维持多久需要 - 不管什么超时参数设置的?
这里就是Ajax调用正在取得的代码:
(这try块是在其用于使一个AJAX调用一个特定的URL的目的的功能。url
,jsonToSend
,和timeout
是所有的参数给此函数。(具体地说,timeout
是一个int,以毫秒为单位的数,以等待之前调用超时)
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
}
// catch块,功能其余..
作为回答@MadWard - 我想如果任何人在这里发表这个不断跌倒在这!这是运行服务器的NodeJS(其中埃杰克斯是发送Ajax调用),似乎有4分钟的默认超时。所以不管我指定什么超时的jQuery制作的电话,如果通话时间超过4分钟,服务器本身会出错误。
由于这是我自己的服务器,我可以通过简单地增加服务器的NodeJS本身的“超时”属性来解决这个问题。下面是一个例子(从那里正在定义的服务器的NodeJS的文件):
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!)