2

More explanation here : Im importing products to my DB, and everytime a product is imported I display the percent calculated earlier:

 //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);

On my local everything is working fine : enter image description here

Every time php echo json_encode() I get on XHR a new state 3, and display the response. Code below:

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);

But I'm trying to understand why it's not working on the server ( XHR state 3 is fired only once, at the end of the request ( so the json is malformed ) enter image description here

Here are the headers: enter image description here

This just break my mind, if someone have an idea, even a bad one xD (Btw there is a proxy on the server : VIA:1.1 Alproxy ) it could stop the response untill it end and then send it alltogether ?

user3013440
  • 108
  • 2
  • 11
Ggs
  • 181
  • 15
  • Also i tryed with : `xhr.addEventListener("progress", updateProgress);` , firing the event on local, still not on server... – Ggs Jul 09 '18 at 10:28
  • More informations of Server php ini : implicit_flush => On => On output_buffering => 0 => 0 – Ggs Jul 09 '18 at 13:28

0 Answers0