How do I pass the value of an input :: file by ajax from the view to a controller

0

I have a script which sends the information of the form to the controller, but when using the value of the input file I get that the value of that field is null

"message": "Call to a member function store () on null "

<script>
    function saveAbsence() {
        $.ajax({
            type: 'POST',
            url: '{{url('/emp/newAbsence')}}',
            data: {
                absenceDate: $('#absenceDate').val(),
                note: $('#note').val(),
                absenceDateEnd: $('#absenceDateEnd').val(),
                absenceFile: $('#absenceFile').val()
            },
            success: function (data) {
                console.log( $('#absenceFile'))
                // location.reload();
            },
            error: function (data) {
                showAjaxErrors(data, 'error_msg');
                markError(['absenceDate', 'absenceDateEnd', 'note']);
            }
        });
    }
</script>
{{ Form::open(array('url' => '/saveAbsence'), ['id' => 'absenceForm'], ['enctype' => 'multipart/form-data'], ['files'=> true]) }}

<div class="row">
    <div class="col-lg-10 col-md-10 col-sm-12">
        <div class="form-group">
            {{Form::file('image')}}
        </div>
    </div>
</div>

<div class="modal-footer">
    {{ Form::button('Guardar', ['class' => 'btn btn-primary', 'onClick' => 'saveAbsence()']) }}
</div>
    
asked by gmrYaeL 14.11.2018 в 16:59
source

1 answer

0

First of all, I recommend reading this example . The important thing is that you must pass the certification token to avoid XSS attacks.

So you must add the following to your ajax data.

data: { _token: '{{ csrf_token() }}' }
    
answered by 14.11.2018 в 19:02