如何在Ajax数据上使用分页

时间:2019-04-04 09:27:31

标签: ajax laravel

我想在laravel中使用默认分页,以查看由ajax函数获取的视图。把我弄错了。

ajax功能

public function get_products($id){
    $products = Product::where('category_id',$id)->where('is_hidden','0')->paginate(9);
    $category = Category::find($id);
    $returnHTML = view('products_ajax')->with('products', $products)->with('category', $category)->render();
    return response()->json(array('success' => true, 'html'=>$returnHTML));
}

ajax返回的视图

    <h3>{{$category->name}}</h1>
        <hr>
        <div class="cards">
            @foreach($products as $product)
                <div class="card" data-item-id="{{$product->id}}">
                    <img style="width:50%;" src="{{asset('storages/images/products/'.$product->image)}}">
                    <div class="card-details">
                        <p class="card-brand">{{$product->brand->name}}</p>
                        <p class="card-name" title="Food Name Here Food Name Here Food Name Here Food Name Here">
                            {{$product->code}}
                        </p>


                        <p class="card-price" hidden> {{$product->price}}</p>

                        <p hidden class="card-full-des">
                            {{strip_tags(html_entity_decode($product->description))}}
                        </p>
                        <p class="card-packing">
                            <span>{{$product->packing}}</span>
                        </p>
                        {{-- <p class="card-packing">
                            <span>Packing: 12-8 oz (225g)</span>
                        </p> --}}
                        <div class="card-foot">
                            <button class="mbtn5" onclick="CardAddToCartOrDetails(this, true)">Add to Cart</button>
                            <button class="mbtn4" onclick="CardAddToCartOrDetails(this, false)">Details</button>
                        </div>
                    </div>
                </div>
                @endforeach
                {{$products->links()}}
        </div>
</div>

但我收到此错误:

error

那么,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

MethodNotAllowedHttpException是由使用错误方法调用路由引起的。

即为/users定义GET路由并用POST调用将导致此错误。