Habib Ur Rehman
Habib Ur Rehman

Reputation: 131

Why my ajax request is giving 500 internal error in 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', [\App\Http\Controllers\SuperAdminController::class, 'verifyUserName'])->name('verifyUserName');

controller

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;


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.

Upvotes: 0

Views: 90

Answers (2)

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2834

You may be submitting ajax data to the wrong route type and even the wrong URL (404). So first double-check the path you are submitting to. Second, check whether the route accepts “Post” or “Get” data type. Because submitting post data via Get method or vice versa will results in Laravel ajax giving 500 Internal Server Error.

<input type="hidden" name="_token" value="{{ Session::token() }}">

var request = $.ajax({
    url: "script.php",
    method: "POST",
    data: {  _token : <?php Session::token() ?>},
    dataType: "html" 
});

DATA TYPE RETURNED:

If you are returning Boolean and your ajax code is set for Json or any other data type then chances are that you will stumble upon the internal server error. Try to set the data type for all ajax responses to JSON. If you are using jQuery then set dataType to XML, JSON, script, or HTML like this:

var request = $.ajax({
    url: "/action",
    method: "POST",
    data: { id : userId },
    dataType: "json"
});

Upvotes: 0

SHOAKROM KAMBAROV
SHOAKROM KAMBAROV

Reputation: 36

public function verifyUserName(Request $request){

    if($request->ajax())
    {
        return response()->json(['code'=>'200']);
    }
}

try public function instead

Upvotes: 2

Related Questions