当我搜索产品时,laravel路线会发生变化

时间:2020-11-09 09:15:56

标签: laravel

我正在laravel中创建搜索功能,它对于我的第一个主页工作正常,但是当我单击搜索并移至下一页时。在下一页中,当我搜索路径时,它会发生更改,并且会显示错误404。为什么laravel路由会发生更改。

页面加载:自动完成

转到另一页:productDetail / autocomplete,... / autocomplete可以避免这种情况。

路线:

Route::get('/getProduct','HomeController@getProduct')->name('getProduct');

Route::get('/getProductList/{id}','ProductListController@index')->name('getProductList');

Route::get('/productDetail/{id}','ProductDetailController@index')->name('productDetail');

Route::get('/autocomplete', 'ProductDetailController@fetch')->name('autocomplete');

js:

$(document).ready(function(){
    $('#product_name').keyup(function(){ 
        var query = $(this).val();
        if(query != '')
        {
         var _token = $('input[name="_token"]').val();
         $.ajax({
          url:"autocomplete",
          method:"get",
          data:{query:query, _token:_token},
          success:function(data){
           $('#product_name').fadeIn();  
                    $('#product_list').html(data);
          }
         });
        }
    });
    $(document).on('click', 'li', function(){  
        $('#product_name').val($(this).text());  
        $('#product_list').fadeOut();  
    });  

});

header.blade.php:

<script type="text/javascript">
      var accessurl = '{{ route('autocomplete') }}';
    </script>
    <script type="text/javascript" src="{{ asset('js/home.js')}}"></script>

   <form class="ps-search--header" action="#" method="post">
              {{ csrf_field()}}
              <input class="form-control" type="text" placeholder="Search Product…" id="product_name">
              <button><i class="ps-icon-search"></i></button>
              <div id="product_list"></div>
   </form>

1 个答案:

答案 0 :(得分:0)

使用网址url:"/autocomplete"

此处/代表您的网址localhost/<root>的根

因此,当您在其他页面中时,它仍将从localhost / autocomplete url中搜索