Problems with AJAX return

Asked

Viewed 31 times

0

<script type="text/javascript">
    $(document).ready(function(){
        $('#btn_busca_f').click(function () {
            var data_ini = $('#data_ini').val();
            var data_fim = $('#data_fim').val();
            var cod_recurso = $('#cod_recurso').val();
            var cod_funcionario = $('#cod_funcionario').val();
            var num_op = $('#num_op').val();
            $.ajax({
                type: 'post',
                async: false,
                url: '/intranet/ExportaFimApontamento',
                data:{
                    '_token': '{{csrf_token()}}',
                    'data_ini': data_ini, 
                    'data_fim': data_fim, 
                    'cod_recurso': cod_recurso,
                    'cod_funcionario': cod_funcionario,
                    'num_op': num_op
                },
                erro: function () {
                    alert('erro');
                },
                success: function (data) {
                    return (data);
                    alert('Excel Gerado com Sucesso!');
                }
            });
        });
    });
</script>

this script performs a function in the controller where it generates an excel.xls file, but if I run the function directly in the controler (without going through the page) it works, if I run with the script(up there) it works but does not generate the.xls file, it generates the preview and replay by F12 browser but does not download excel. Does anyone know how to get him to download the file when running the script? or if there is another way? in my form Submit does the function however it is there to consult a report.

1 answer

0


When you receive Success in ajax you already know the download url no?

In the Laravel there is a Sponse for download. see:

https://laravel.com/docs/5.8/responses#file-downloads

After receiving the answer from Success, try to give a:

window.location.href = link_para_download

I’ve never tested it like this, but it might work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.