我有一个页面,允许用户上传和映射 CSV 文件。完成后,行将通过后调用发送到不同服务器上的页面。经过近 6,00 次调用(确切地说是 5787 次)后,我开始收到控制台错误“无法加载资源:net::ERR_INSUFFICIENT_RESOURCES”。
我尝试过在 CSV 文件中运行包含 100 行的页面,效果很好...但是当我尝试使用大型列表(超过 10,000 行)时,它就卡住了。
这是进行 post 调用的代码:
for (var i = 0; i < manifestRows.length; i++)
{
$.post('http://www.otherserver.com/subfolder/handler.php', { tblName: 'foobar', thing1: manifestRows[i][0], thing2: manifestRows[i][1], thing3: manifestRows[i][2], thing4: manifestRows[i][3], thing5: manifestRows[i][4], thing6: manifestRows[i][5], thing7: manifestRows[i][6], thing8: manifestRows[i][7], thing9: manifestRows[i][8], thing10: manifestRows[i][9], thing11: manifestRows[i][10], thing12: manifestRows[i][11], thing13: manifestRows[i][12], thing14: manifestRows[i][13], thing15: manifestRows[i][14], thing16: manifestRows[i][15], thing17: manifestRows[i][16], thing18: manifestRows[i][17] }, function(data) {
if (data.length == 0)
{
var currentProcessing = $('#processingCurrent').html();
$('#processingCurrent').html(parseInt(currentProcessing) + 1);
var progress = Math.ceil((parseInt(currentProcessing) / manifestRows.length) * 100);
if (progress == 99)
progress = 100;
progress = progress + '%'
$("#progressBar").width(progress).html(progress);
if (progress == '100%')
{
$('#processdingDiv').hide();
$('#allDone').show();
}
}
else
alert(data);
});
}
是否可以在用户端或其他服务器上放置一些代码来防止发生此资源不足错误?
最佳答案
我在 AngularJS 应用程序中遇到了几乎完全相同的错误。我要做的就是批量处理请求。批处理号与您的实例相关,但我一次使用了 1,000 个调用。
由于很多事情都是异步发生的,我必须创建两个变量。 httpRequestsExpected
应与批量大小相同,具体取决于您的代码。在我的例子中,我只是在每次调用 $http.$get();
时增加它以获得准确的值。
var batchSize = 1000;
var httpRequestsExpected;
var httpRequestsMade = 0;
然后在http成功和错误函数中,递增httpRequestMade
。
要解析批处理,并且由于 http 有时会挂起,我无法进行精确匹配,例如:
if(httpRequestsMade === httpRequestsExpected)
但必须像这样添加填充:
if(httpRequestsMade >== httpRequestsExpected - (httpRequestsExpected *0.01))
然后开始下一个批处理,将起始指针增加 batchSize
并重置变量。这为进程提供了安全的缓冲量和时间来完成,而不会消耗所有资源。
关于jQuery post 调用导致 "Failed to load resource: net::ERR_INSUFFICIENT_RESOURCES",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161236/