使用ajax脚本laravel动态获取数据时,我的代码存在一些问题

时间:2019-05-13 09:06:32

标签: javascript jquery ajax laravel laravel-5.1

我正在一个项目上,我需要一个“加载更多”按钮,我尝试了以下代码,但我认为出了点问题。

点击查看全部时,会显示更多加载按钮,应该会显示更多结果。在单击查看全部时,它调用了控制器方法,但是我认为我无法数据发布到控制器方法中,我将从ajax响应中获得什么。

以下是我尝试的代码。

查看

<div class="form-group border-bottom pb-3">
                <label for="usr" class="f-14 bold black">Manufacturers</label>

                <div class="pl-md-4 pl-2">
                    {{count($leedManufacturers)}}
                    <label class=" my-checkbox gry2">Show All Manufacturers
                        <input type="checkbox">
                        <span class="checkmark"></span>
                    </label>
                    @if(count($leedManufacturers) > 0 )
                    @foreach($leedManufacturers as $leedsManufacturer)
                    {{-- @foreach($leedManufacturers as $leedsManufacturer)  --}}

                    <label class=" my-checkbox gry2" id="manufacturer">{{str_limit($leedsManufacturer['name'], 300)}}
                            <input type="checkbox">
                            <span class="checkmark"></span>
                        </label>
                        {{-- for load more script --}}
                    {{-- <input type="hidden" id="row" value="0"> --}}

                    {{-- <input type="hidden" id="all" value="{{$total_manufacturers}}"> --}}
                    @endforeach
                    @endif

                    <a class="load-more" id="load" href="#" class="f-14 bold">See All</a>

                    <input type="hidden" id="row" value="0">
                    <input type="hidden" id="all" value="{{count($leedManufacturers)}}">


                </div>
            </div>

脚本

 <script type="text/javascript">
        $(document).ready(function(){

        // Load more data
        $('.load-more').click(function(){
            var row = Number($('#row').val());
            var allcount = Number($('#all').val());
            var rowperpage = 3;
            row = row + rowperpage;

            if(row <= allcount){
                $("#row").val(row);

                $.ajax({
                    url: "{{url('GuestController/colabrativetwo')}}",
                    type: 'post',
                    data: {row:row},

                    success: function(data){

                        alert(data);

                    }


                });

            }else{

                console.log('if is not working');



            }

        });

        });
</script>

控制器:

public function colabrativetwo(Request $request, $slug = 0, $division_id = 0, $section_id = 0, $manufacturer_id = 0, $list=""){

$row = $request->row;
      $manufacturersFeatured = Product::select('manufacturers.id AS id', 'manufacturers.name AS name', DB::raw('COUNT(DISTINCT products.id) as products_count'))
                ->whereIn('products.status', ['active'])
                ->where(function ($query) {
                    $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
                })
                ->where('users.user_type', 'manufacturer_paid')
                ->leftJoin('products_selected_leeds', 'products.id', '=', 'products_selected_leeds.product_id')
                ->leftJoin('sections', 'products.section_id', '=', 'sections.id')
                ->leftJoin('divisions', 'products.division_id', '=', 'divisions.id')
                ->join('manufacturers', 'products.manufacturer_id', '=', 'manufacturers.id')
                ->join('users', 'manufacturers.user_id', '=', 'users.id')
                ->orderBy('name', 'asc')
                ->groupBy('manufacturers.id')
                ->limit($row)
                ->get();


 return view('guest::new_design_frontend.product-library', $manufacturersFeatured);}

1 个答案:

答案 0 :(得分:0)

尝试在标题中添加csrf_token。      并在ajax调用中使用它。

$.ajax({
    url: "{{url('GuestController/colabrativetwo')}}",
    type: 'post',
    headers: {
         'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
    },
    data: {row:row},
    success: function(data){
        alert(data);
    }
 });