Why my ajax request is giving 500 internal error in laravel

  ajax, jquery, laravel

I am trying to varify username when new user is entered. and i am trying to use ajax so user can given correct username in the field.

form

<form role="form">
     <input type="hidden" id="token" name="_token" value="{{csrf_token()}}">

        <div class="form-row">
             <div class="form-group col-md-6">
                  <label for="firstname">First Name</label>
                  <input type="text" class="form-control" id="firstname" placeholder="First Name">
             </div>
             <div class="form-group col-md-6">
                  <label for="lastname">Last Name</label>
                  <input type="text" class="form-control" id="lastname" placeholder="Last Name">
             </div>

             <div class="form-group col-md-6">
                  <label for="username">User Name</label> <label id="username_message" style="float: right;margin-right: 10px"></label>
                  <input type="text" class="form-control" id="username" placeholder="User Name">
             </div>
             <div class="form-group col-md-6">
                  <label for="email">Email</label>
                  <input type="email" class="form-control" id="email" placeholder="Email">
             </div>

             <div class="form-group col-md-6">
                  <label for="pass">Password</label>
                  <input type="password" class="form-control" id="pass" placeholder="Password">
             </div>


        </div>

        <div class="text-center">
             <button type="submit" name="submit" id="insert" class="btn btn-primary my-4">{{ __('Sign in') }}</button>
        </div>
</form>

ajax request

    $(document).ready(function (){
        $('#username').keyup(function (){
            var token = $('#token').val();
            var username = $('#username').val();

            if (username !== '') {

                $.ajax({
                    type: 'get',
                    url: '{{ route('verifyUserName') }}',
                    data: {
                        '_token': token,
                        'username': username,
                    },

                    success: function (response) {
                        console.log('ok');

                    }
                });
            }
        });
    });

route

Route::get('verifyUserName', [AppHttpControllersSuperAdminController::class, 'verifyUserName'])->name('verifyUserName');

controller

<?php

namespace AppHttpControllers;

use AppModelsUser;
use IlluminateHttpRequest;


class SuperAdminController extends Controller
{
    private function verifyUserName(Request $request){
        if($request->ajax())
        {
            return response()->json(['code'=>'200']);
        }
    }
}

I have tried also the post method but the result is same. it is giving me this error.

error

jquery.min.js:2 GET http://localhost/Project/verifyUserName?_token=duyglp2z8DhpAJ8vnI4rd1Vrdjnqzq1zw1ythiZa&username=g 500 (Internal Server Error)     

jquery.min.js:2

i am very confused what is the problem. please help me. thanks in advance.

Source: Laravel

Leave a Reply