我正在使用此Ajax代码来检查域.对于每个域,将请求发送到API.我在具有3个后缀(6000个域)的textarea中创建2000行,然后单击Submit.提交所有域后,选中并使用ajax在表中显示域状态.第一次显示域表,但过了几秒钟后,域表被删除,代码不显示结果!
如何解决这个问题?
Failed to load resource: net::ERR_INSUFFICIENT_RESOURCES
Ajax代码(ajax.js):
$(document).ready(function () {
$("#submit").click(function () {
// check if anything is selected:
if(!$('#domains').val() || !$('[type="checkBox"]:checked').length){
return false;
}
// disable the button:
var btn = $(this).prop('disabled', true);
var domain = $('#domains').val().split("\n");
var counter = 0;
// an indicator to state when the button should be enabled again:
var ajaxQueue = 0;
//send ajax request for earse txt file (new request)
$.ajax({
type: "GET",
url: "includes/ajax/ajax.PHP",
data: {new_request: ajaxQueue },
});
var Table = '<table class="paginated table table-bordered table-striped table-responsive domain-table"><thead><tr><th>ID</th><th>Domain Name</th><th>.Com</th><th>.Net</th><th>.Org</th><th>.Ir</th><th>.Biz</th><th>.Info</th><th>.Us</th><th>.Name</th><th>.Pro</th><th>.Eu</th><th>.In</th><th>.Me</th><th>.Tv</th><th>.Cc</th></tr></thead><tbody>';
// create the td elements, but do not perform AJAX requests there:
$.each(domain, function (i, val) {
counter++;
Table += '<tr><td>'+ counter +'</td><td>'+ val +'</td>';
$('input[type=checkBox]').each(function () {
if($(this).is(':checked')){
ajaxQueue++;
// if checkBox is checked make td element with specified values and a "load-me" class:
Table += '<td class="load-me" data-domain="'+val+'" data-suffix="'+$(this).val()+'"><small>loading...</small></td>';
}else{
Table += '<td><span class=text-muted><i class="fa fa-minus"></i></span></td>';
}
});
Table += '</tr>';
});
// Replace HTML of the 'domain_tables' div and perform AJAX request for each td element with "load-me" class:
$('#domain_tables').html(Table+'</tbody></table>').find('td.load-me').each(function(){
var td = $(this);
$.ajax({
type: "POST",
url: "includes/ajax/ajax.PHP",
dataType: "json",
data: {domain: td.attr('data-domain'), suffix: td.attr('data-suffix')},
success: function (msg) {
// decrease ajaxQueue and if it's 0 enable button again:
ajaxQueue--;
if(ajaxQueue === 0){
btn.prop('disabled', false);
}
if(msg.suc == false){
td.html('<span class=text-danger><i class="fa fa-check"></i></span>');
}else{
td.html('<span class=text-success><i class="fa fa-times"></i></span>');
}
},
error: function (err) {
$('#domain_tables').html(err.error);
}
});
});
// clear textarea and uncheck checkBoxs
$("#reset").click(function(){
$('input[type=checkBox]').attr('checked', false);
$('#domains').val('');
$('#submit').prop('disabled', false);
});
// table paganation
$('table.paginated').each(function() {
var currentPage = 0;
var numPerPage = 100;
var $table = $(this);
$table.bind('repaginate', function() {
$table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show();
});
$table.trigger('repaginate');
var numRows = $table.find('tbody tr').length;
var numPages = Math.ceil(numRows / numPerPage);
var $pager = $('<ul class="pager pagination"></ul>');
for (var page = 0; page < numPages; page++) {
$('<li class="page-number"></li>').text(page + 1).bind('click', {
newPage: page
}, function(event) {
currentPage = event.data['newPage'];
$table.trigger('repaginate');
$(this).addClass('active').siblings().removeClass('active');
}).appendTo($pager).addClass('clickable');
}
if(numRows > 100 ){
$pager.insertAfter($table).find('span.page-number:first').addClass('active');
}
});
});
});
<?PHP
if($_SERVER['REQUEST_METHOD']=='GET'){
$new_request = $_GET['new_request'];
// check new request flag for erase all data from txt file
if(isset($new_request) && $new_request == 0 ){
$handle = fopen ("../textfile/data.txt", "w+");
fclose($handle);
}
}
if($_SERVER['REQUEST_METHOD']=='POST'){
$domain = $_POST['domain'];
$suffixes = $_POST['suffix'];
$target = 'http://whois.apitruck.com/'.$domain.".".$suffixes;
$getcontent = file_get_contents($target);
$json = json_decode($getcontent);
$status = $json->response->registered;
if($status){
die(json_encode(array('suc'=>true)));
} else {
$file = '../textfile/data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $domain.".".$suffixes." | \n";
// Write the contents back to the file
file_put_contents($file, $current);
die(json_encode(array('suc'=>false)));
}
}
?>
解决方法:
根据Chrome浏览器的代码审查,导致该错误的原因是,您达到了对呈现的流程可以处理的请求数量的约束,并且正如您提到的那样,您正在谈论的是〜6000个请求.
错误:
net::ERR_INSUFFICIENT_RESOURCES
原因:
Add a constraint on how many requests can be outstanding for any given
render process (browser-side).Once the constraint is reached, subsequent requests will fail with
net::ERR_INSUFFICIENT_RESOURCES.The bound is defined as “25 MB”, which represents the amount of
private bytes we expect the pending requests to consume in the
browser. This number translates into around 6000 typical requests.Note that the upload data of a request is not currently considered
part of the request’s in-memory cost — more data is needed on the
average/maximum upload sizes of users before deciding what a
compatible limit is.
来源:https://codereview.chromium.org/18541
无论如何如何解决?
我的建议是接受输入并将其添加到数据库中,放入一个pending_requests表中.在您的服务器中,创建一个每隔几分钟运行一次的cronjob并完成该表中的X个请求,一旦完成,请从该表中删除它们,以便下次cronjob运行时将转到下一个X请求.
与多重请求一起使用的许多服务都以某种方式使用这种方法.他们通常在任务完成时通过电子邮件通知用户.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。