insomniac22
insomniac22

Reputation: 151

Ajax search producing 404 error Laravel 7

I have a ajax search function working in laravel 5 that wont work on laravel 7. I was working on an old version to learn and upgraded everything and now I am getting a 404 error in the ajax when posting data to my route. I am very new to Laravel so still learning and modifying code to test outcomes. Basically I am trying to pass two variables to the controller, the code all works in the older version. Any help and direction would be greatly appreciated.

Routes

Route::get('/search','SearchController@index');
Route::get('/search/action','SearchController@search')->name('search.action');

Controller

 public function search(Request $request)
    {
        if ($request->ajax()) {
            $output = "";
            $figures = figures::where('type', 'LIKE', '%' . $request->search . "%")->where('base', 'LIKE', '%' . $request->txt . "%")->get();
            $prodcount = $figures->count();
            if ($prodcount >= '1') {

                }
                return Response($output);
            } else {

                }
                return Response($output);
            }
        }
    }

Ajax

    <script type="text/javascript">
        //$('#search').on('keyup',function(){
  //  $("#topping").change(function () {
    $("#but").click(function () {
        $value=$('#topping').val();
        $value2=$('#search').val();
            $.ajax({
                type : 'get',
               url : '{{URL::to('search/action')}}',
                data:{'search':$value, 'txt':$value2},
                success:function(data){
                    $('tbody').html(data);
//console.log(data);
                }
            });
    })
    </script>
    <script type="text/javascript">
        $.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
    </script>

Upvotes: 0

Views: 50

Answers (1)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

i can see 2 errors

  • $value2=$('#search').val(); to var value2=$('#search').val();
  • url : '{{URL::to('search/action')}}', to url : {{ route('search.action') }}
<script type="text/javascript">
    //$('#search').on('keyup',function(){
//  $("#topping").change(function () {
$("#but").click(function () {
    var value=$('#topping').val();
    var value2=$('#search').val();
        $.ajax({
            type : 'get',
           url : '{{ route('search/action')}}',
            data:{'search':value, 'txt':value2},
            success:function(data){
                $('tbody').html(data);
                //console.log(data);
            }
        });
})
</script>
<script type="text/javascript">
    $.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>

this is fixed code try it

Upvotes: 1

Related Questions