Using Laravel 7 jQuery v3.4.1
I'm trying to send the content of a form via Ajax to a Laravel Controller. The form contains a text field and a file field . I wanted to find a way to send both types of fields in one request.
It looks like the content gets send but Laravel can't read it and returns an error.
My Ajax Code
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var form = $('form')[0];
var formData = new FormData(form);
$.ajax({
url: form.attr('action'),
data: formData,
cache: false,
method:'POST',
contentType: false,
processData: false,
success: function (data) {
console.log(data);
},
error: function (error,jqXHR, textStatus, errorThrown) {
console.log(data.error);
}
});
The Form
<form id="newProduct" action="{{route('Product.store',app()->getLocale())}}" method="POST" enctype="multipart/form-data">
@csrf
<input name="title" maxlength="10" length="10" placeholder="Product Title" type="text" id="title" class="form-control>
<input type="file" name="files" id="files" class="files">
</form>
The Laravel function
public function store(Request $request)
{
dd($request->input->all());
}
The Data gets send via ajax , but somehow Laravel can't access it all. I get the following error message from Laravel Call to a member function all() on null with a 500 Http Return code.
This is an extract from the http request send to Larval via ajax
-----------------------------23552347725043592001228805073 Content-Disposition: form-data; name="title" sdf
What has to be changed so Laravel can read the data ? Thanks in advance.
dd($request->all());