XHR和php进度条,在本地工作,而不是在远程工作
更多说明如下: 我正在将产品导入数据库,每次导入产品时,我都会显示前面计算的百分比:XHR和php进度条,在本地工作,而不是在远程工作,php,xmlhttprequest,progress-bar,Php,Xmlhttprequest,Progress Bar,更多说明如下: 我正在将产品导入数据库,每次导入产品时,我都会显示前面计算的百分比: //Progress bar set_time_limit(0); ob_implicit_flush(true); ob_end_flush(); sleep(1); $p = ($i/$count_product)*100; //Progress $response = array( 'success' => true , 'message' =&
//Progress bar
set_time_limit(0);
ob_implicit_flush(true);
ob_end_flush();
sleep(1);
$p = ($i/$count_product)*100; //Progress
$response = array( 'success' => true , 'message' => $p . '% ','progress' => $p);
$i++;
echo json_encode($response);
在我的本地电脑上,一切正常:
每次php echo json_encode()我都会在XHR上得到一个新的状态3,并显示响应。
代码如下:
var xhr = new XMLHttpRequest();
xhr.previous_text = '';
$param = 'id='+$(this).find('input').val();
xhr.onerror = function() {console.log(xhr.responseText) };
xhr.onreadystatechange = function() {
//try{
console.log(xhr.readyState);
console.log(xhr);
console.log(new_response);
if (xhr.readyState == 4){
var new_response = xhr.responseText.substring(xhr.previous_text.length);
var result = JSON.parse( new_response );
if (result['message'] == '100' ) {
$('.progress_bar').html('<div><?=$this->translate->_("Imported succefully")?></div>');
$.when($('.full_screen_layer').fadeOut(2000)).done(function() {
location.reload();
});
}else{
$('.progress_bar').html('<div style="color:red">'+result['message']+'</div>');
setTimeout(function(){
$.when($('.full_screen_layer').fadeOut(2000)).done(function() {
location.reload();
});
}, 2000);
// alert('<?=$this->translate->_("Please try again")?>');
}
}
else if (xhr.readyState > 2){
var new_response = xhr.responseText.substring(xhr.previous_text.length);
var result = JSON.parse( new_response );
if (result['success'] && result['end'] == undefined) {
$('.progress_bar').html('<div>'+result['message']+'</div>');
xhr.previous_text = xhr.responseText;
}else{
$('.progress_bar').html('<div style="color:red">'+result['message']+'</div>');
}
}
//}
// catch (e){
// alert("[XHR STATECHANGE] Exception: " + e);
// }
};
xhr.open("POST", action, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send($param);
var xhr=new-XMLHttpRequest();
xhr.previous_text='';
$param='id='+$(this.find('input').val();
xhr.onerror=function(){console.log(xhr.responseText)};
xhr.onreadystatechange=函数(){
//试一试{
console.log(xhr.readyState);
console.log(xhr);
console.log(新的_响应);
if(xhr.readyState==4){
var new_response=xhr.responseText.substring(xhr.previous_text.length);
var result=JSON.parse(新的_响应);
如果(结果['message']='100'){
$('.progress_bar').html('');
$.when($('.full_screen_layer').fadeOut(2000)).done(函数(){
location.reload();
});
}否则{
$('.progress_-bar').html(''+result['message']+'');
setTimeout(函数(){
$.when($('.full_screen_layer').fadeOut(2000)).done(函数(){
location.reload();
});
}, 2000);
//警报(“”);
}
}
否则如果(xhr.readyState>2){
var new_response=xhr.responseText.substring(xhr.previous_text.length);
var result=JSON.parse(新的_响应);
if(结果['success']&&result['end']==未定义){
$('.progress_-bar').html(''+result['message']+'');
xhr.previous_text=xhr.responseText;
}否则{
$('.progress_-bar').html(''+result['message']+'');
}
}
//}
//捕获(e){
//警报(“[XHR STATECHANGE]异常:”+e);
// }
};
xhr.open(“POST”,action,true);
setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send($param);
但我试图理解它为什么不能在服务器上工作(XHR状态3只在请求结束时触发一次(因此json格式不正确)
以下是标题:
这让我心碎,如果有人有主意,即使是坏主意
(顺便说一句,服务器上有一个代理:VIA:1.1 Alproxy)它可以停止响应,直到响应结束,然后一起发送?我还尝试了:
xhr.addEventListener(“progress”,updateProgress)
,在本地触发事件,但仍然不在服务器上…有关服务器php ini的更多信息:隐式\u flush=>on=>on输出\u缓冲=>0=>0