【问题标题】:Getting no response from ajax searching with laravel使用 laravel 进行 ajax 搜索没有响应
【发布时间】:2017-04-05 10:56:34
【问题描述】:

我的项目中出现 ajax 搜索问题。

这是我的看法:

<div class="col-md-12">
            {!! Form::open(['class' => 'form-horinzontal']) !!}
            {!! Form::text('search', null, array('id' => 'search-input','required','class'=>'form-control','placeholder'=>'Product zoeken, begin met typen...','onkeyup' => 'search_data(this.value, "result")', 'autofocus')) !!}
            {!! Form::close() !!}
            <br>
            <script>
                function search_data(search_value) {

                    console.log(search_value);
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                        }
                    });
                    $.ajax({
                        type : 'get',
                        url : '{{ URL::to('/webshop/products/searchProduct') }}',
                        data : {'search':search_value},
                        success:function(data) {
                            console.log(data);
                        }
                    });
                }
            </script>
        </div>

控制器:

public function searchProduct(Request $request){

    if($request->ajax()) {
        $output = "";
        $products = Product::where('beschrijving', 'LIKE','%'. $request['search'] .'%')->get();

        if(count($products)) {
            foreach($products as $product){
                $output .= '<tr>' .
                           '<td>' . $product->beschrijving . '</td>' .
                           '<td>' . $product->artikelcode . '</td>' .
                           '<td>' . $product->prijs . '</td>' .
                           '</tr>';
            }
            return response($output);
        } else {
            return response('TESTING!');
        }
    }
}

还有路线:

Route::group(['prefix' => '/webshop/products'], function() {
        Route::get('/searchProduct', 'ProductsController@searchProduct');
    });

我得到一个完全空白的回复,这很奇怪。

我也尝试返回字符串而不是 $ouput 变量,控制器什么也不返回。

我也没有遇到任何错误,而且我没有选择。 我得到的回应:

【问题讨论】:

    标签: php jquery ajax laravel-5


    【解决方案1】:

    我的路线错了:

    必须从前缀组中删除 get,然后它才起作用。

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 2019-08-09
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2014-02-05
      • 2019-07-15
      相关资源
      最近更新 更多