Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

germanraffo's avatar

405 error - Method not allowed

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.

0 likes
3 replies
trin's avatar

u can check http query from chrome console, what you send, what you get. u can check request in laravel telescope. if it doesn't help, post your raw request here like

> POST /markdown HTTP/2
> Host: laracasts.com
> accept-encoding: deflate, gzip, br
> authority: laracasts.com
> pragma: no-cache
> cache-control: no-cache
> accept: application/json, text/plain, */*
> x-xsrf-token: `some token`
> x-requested-with: XMLHttpRequest
> user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
> content-type: application/json;charset=UTF-8
> origin: https://laracasts.com
> sec-fetch-site: same-origin
…

p.s.

$validation = Validator::make(request()->all(), [
	'profile_picture' => ['image', 'mimes:jpeg,png,jpg,gif', 'max:1024']
]);

it's useless first, u send image in base64. second, u dont send profile_picture. and $validation->passes() is true only because u havent require rule

viorel's avatar

I suppose that the file input is inside a form of some sorts. If it's a form you probably have the action set to GET and when you click the button it should submit it before the ajax request goes through and then refreshes the page. I cannot see the event.preventDefault() in the javascript code you posted. Sorry if my assumption is wrong. It would be helpful to post also the html for the form.

1 like

Please or to participate in this conversation.