Hi! How are you?
Im having some trouble with an ajax image upload, i get 405 error - method not allowed when i try to run the next piece of code:
My route looks like this:
Route::post('/mi-perfil/update_profile_picture', 'UsersController@updatePic')->name('update_profile_picture');
The script that calls the ajax method looks like this:
$('#cropPicture').on('click', function(ev){
$(this).attr('disabled', 'true');
$(this).html('Procesando...');
resize.croppie('result',{
type: 'canvas',
size: 'viewport'
}).then(function(img){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '/mi-perfil/update_profile_picture',
method: 'POST',
data: {'image' : img},
success: function(data)
{
$('#cropPicture').attr('disabled', false);
$('#cropPicture').html('Recortar foto de perfil');
$('#crop_popup').hide();
$('body').toggleClass('no-scroll');
$('.profile_picture_message').html(data.message);
if(data.currImg){
var url = "{{ getStatic('storage/avatars/') }}" + "/" + data.currImg;
$('.profile_picture').attr('src', url);
}
}
});
});
});
And my controller looks like this
public function updatePic(Request $request)
{
$user = Auth::user();
$actualPicture = $user->profile_picture;
$data = $request->image;
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$validation = Validator::make(request()->all(), [
'profile_picture' => ['image', 'mimes:jpeg,png,jpg,gif', 'max:1024']
]);
if($validation->passes()){
if($actualPicture != 'user.jpg'){
Storage::delete('avatars/'.$user->profile_picture);
$avatarName = $user->id.'_avatar_'.time().'.png';
$save = Storage::put("avatars/{$avatarName}", $data);
$user->profile_picture = $avatarName;
$user->save();
}else{
$avatarName = $user->id.'_avatar_'.time().'.png';
$save = Storage::put("avatars/{$avatarName}", $data);
$user->profile_picture = $avatarName;
$user->save();
}
return response()->json([
'message' => "Imagen actualizada correctamente.",
'currImg' => $avatarName
]);
}else{
return response()->json([
'message' => $validation->errors()->all(),
]);
}
}
All used to work fine, but now i get that error.