Adhik Mulat
Adhik Mulat

Reputation: 538

Live Search using Ajax and Getting Error "500 (Internal Server Error)" on Laravel 5.8

I was following tutorial to Live Search using ajax on Laravel, but in the implementation I get error:

GET http://localhost:8000/search?search=k 500 (Internal Server Error)

I was following this tutorial 3 times but always getthis same error. I modified like this:

<!DOCTYPE html>
<html>
    <head>
        <meta name="_token" content="{{ csrf_token() }}"> 
        <title>Live Search</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3>Products info </h3>
                    </div>
                    <div class="panel-body">
                        <div class="form-group">
                            <input type="text" class="form-controller" id="search" name="search">
                            <input type="hidden" name="_method" value="POST">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        </div>
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                  <th>ID</th>
                                  <th>Product Name</th>
                                  <th>Description</th>
                                  <th>Price</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <script type="text/javascript">
            $('#search').on('keyup',function() {
                $value=$(this).val();
                $.ajax({
                    type : 'get',
                    url : '{{URL::to('search')}}',
                    data:{'search':$value},
                    success:function(data){
                        $('tbody').html(data);
                    }
                });
            });
        </script>

        <script type="text/javascript">
            $.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
        </script>
    </body>
</html>

my controller:

public function search(Request $request)
{
    if($request->ajax()) {
        $output="";
        $products=DB::table('products')->where('title','LIKE','%'.$request->search."%")->get();
        if($products) {
            foreach ($products as $key => $product) {
                $output.='<tr>'.
                  '<td>'.$product->id.'</td>'.
                  '<td>'.$product->title.'</td>'.
                  '<td>'.$product->description.'</td>'.
                  '<td>'.$product->price.'</td>'.
                '</tr>';
            }
            return Response($output);
        }
    }
}

I was trying this code for 3 different database and always get the same error 500 .

Upvotes: 0

Views: 730

Answers (2)

iLaxo Mit
iLaxo Mit

Reputation: 87

You have call ajax using get method, so first check your route file.

I think you are calling search method using post method.

Also in ajax code default is get method

you have to specify : method : post

Upvotes: 1

user11340725
user11340725

Reputation:

you need to declare variable in Jquery/Javascript like below :

 var value=$(this).val();

and pass this variable like below in ajax :

data:{'search':value}

Change above lines ,it should work !

Upvotes: 0

Related Questions