php - Can't export file excel in Maatwebsite laravel using ajax

one text

I have a problem that I would like you to help me with. I export excel in ajax laravel using maatwebsite . But when I executing the program, the erros function always calls instead of the susscess, if I don't use ajax the file is downloaded normally. I tried many times but still can't download the excel file. Thank you!

Controller:

use Maatwebsite\Excel\Facades\Excel as FacadesExcel;

private function exportDataFile(){
    return  (new FacadesExcel())::download(Members::all(), 'users.xlsx');
}

ajax:

jQuery(document).ready(function() {
    jQuery('#ajaxExport').click(function(e) {
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            }
        });
        jQuery.ajax({
            url: "{{ url('/members-gifts-export') }}",
            method: 'post',
            dataType: "json",
            xhrFields: {
                responseType: 'blob'
            },
            data: {
                giftsType: typeGiftPosition,
                giftstime: yearGifts,
                giftsContent: jQuery('#gifts-content').val(),
            },
            success: function(response) {
                console.log("success");
            },
            error: function(data) {
                console.log("error");
            }
        });
    });
});

Html:

<form action="{{ url('/members-gifts-export') }}" method="post">
                    @csrf
                    <div class="row">
                        <div class="form-group col-md-12">
                            <label class="control-label">Type</label>
                            <input class="form-control" type="text" required
                                id="gifts-type" disabled>
                            <label class="control-label" style="margin-top: 10px;">Year</label>
                            <input class="form-control" type="number" id="gifts-time" required value="2022">
                            <label class="control-label" style="margin-top: 10px;">Content</label><br>
                            <div style="width: 100%;background-color: #767777;margin-bottom: 5px;">
                                <img class="control-label" src="{{asset('img/excel_title.jpg')}}"
                                    style="width: 100%;height: 100%;">
                            </div>
                            <input class="form-control" type="text" id="gifts-content" required>
                        </div>
                    </div>

                    <button class="btn btn-save" type="submit" id="ajaxExport">Export File</button>

                    <a class="btn btn-cancel" data-dismiss="modal" id="modal-export-cancel" href="#">Cancel</a>
                </form>

Result: Result console

Version:

  • "php": "^8.0.2"
  • "maatwebsite/excel": "^3.1"
  • Laravel Framework 9.35.1

Source