In my application I need to upload a cover image for a dish.
Here's my AJAX call:
$('#addDishForm').submit(function(e) {
e.preventDefault();
var el = $(this);
var formAction = el.attr('action');
var formValues = el.serialize();
console.log(formValues);
$.ajax({
type: 'POST',
url: '/admin/menu',
data: new FormData(el[0]),
success: function (data) {
console.log(data);
},
error: function(data) {
console.error(data);
}
});
});
I have an error as a result return
from controller in console.
Uncaught TypeError: Illegal invocation
What is the workaround?
Here is the controller's method:
public function store(Request $request)
{
if($request->hasFile('cover_image'))
{
return response()->json([ 'success' => true, 'message' => 'File exists' ]);
}
else
{
return response()->json([ 'success' => false, 'message' => 'File doesn\'t exist' ]);
}
}
A route:
Route::middleware('auth')->prefix('/admin')->namespace('Admin')->group(function () {
// other routes ...
Route::post('/menu', 'MenuController@store');
// other routes ...
});
After the response in console I can the that success
is false
.
Here's the form:
{!! Form::open([ 'method' => 'POST', 'enctype' => 'multipart/form-data', 'id' => 'addDishForm' ]) !!}
// other form controls ...
// other form controls ...
{!! Form::close() !!}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire