funtus
funtus

Reputation: 19

Laravel: Ajax using problem always return error 500

Good day. Trying to send GET method using Ajax and always getting error 500. CSRF used but I can't find where is error. This is my code:

blade

.....
<meta name="csrf-token" content="{{ csrf_token() }}">
.....
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            let infoModal = document.getElementById('Modal_UserEdit');
            infoModal.addEventListener('show.bs.modal', function () {
                let userId = document.getElementById("_Input_UserID").value;

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    url: "{{ route('settings.user.show') }}",
                    type: 'GET',
                    data: { '_token': '{{ csrf_token() }}', id: userId },
                    success: function(data) {

                        console.log(data);

                    }
                });
            });
        });
    </script>

web.php

Route::get('settings/users/user/show', [UserController::class, 'show'])->name('settings.user.show');

UserController.php

    public function show(Request $request)
    {
        $form = $request->only(['id']);
        $user = DB::table('users')->where('id', $form['id'])->first()->toJson();
        return $user;
    }

Don't think this is CSRF problem, because:

  1. If inside UserController just ask something like dd($form) it returns
  2. If disable CSRF in Kernal.php - nothing changed...

Somebody please help to understand what I'm doing wrong? Thank you!

Upvotes: 0

Views: 161

Answers (1)

funtus
funtus

Reputation: 19

Fixed. Thank for log reminder )))

Fixed code:

    public function show(Request $request)
    {
        $form = $request->only(['id']);
        $user = DB::table('users')->where('id', $form['id'])->first();
        return json_encode($user);
    }

Upvotes: 1

Related Questions