Do not stop AJAX requests when reloading page

0

I have a csv file that has enough records (40,000), I read it with js and I convert it to an array where I fragment it to make different ajax calls. my doubt is on the one hand concepts. It turns out that being a lot of records ajax calls take a long time, and I would like to leave that page and that the processes that occur in the backend with php keep running as they were called, but when I stop the page the ajax requests are canceled and with this the processes, being that in my view, should not affect since they are two processes that are separate, I hope to get out of this without a cron.

So my first question is how to make those requests not be canceled when I stop the page, I hope the user can close the page and not cancel the processes. and second if it is possible to solve the first, how to reload the page without waiting for the requests to finish, since the reload is not executed until at least 1 of the 6 requests ends (which apart point are filling data in a excel sheet).

function callService(filename) {
    var arraySend = [];
    arrayCsv.splice(0, 1);
    var size = Math.ceil(arrayCsv.length / 6);
    for (var i = 0; i < 6; i++) {
        arraySend = [];
        arraySend = moverElementos(arrayCsv, arraySend, size);
        $.ajax({
            url: "<?php echo base_url(); ?>" + "index.php/tools/neitcomProcess",
            type: "POST",
            dataType: 'json',
            data: { 'dataArray': arraySend, 'filename': filename },
            cache: false
        }).done(function (res) {
            console.log(res);
        });
    }
    window.location.reload();
}

Thanks in advance.

    
asked by Yilo 21.11.2017 в 06:26
source

1 answer

0

It depends on the backend engine you are using to process PHP calls. I use nginx with php-fpm and use fastcgi_finish_request as follows:

  ... acá defino si recibí bien el archivo ...

  ob_start();
  echo "Archivo Recibido";
  $content = ob_get_flush();
  fastcgi_finish_request(); // Se termina el request y se envía la respuesta

  ... acá proceso el archivo
    
answered by 21.11.2017 в 12:38