【问题标题】:Ajax Pagination Issue in Laravel 5.2Laravel 5.2 中的 Ajax 分页问题
【发布时间】:2016-10-02 00:20:40
【问题描述】:

我想让默认的 laravel 分页不通过 ajax 刷新页面但出现错误,非常感谢您的帮助。 当我点击第 2 页时,它显示 500 错误

GET http://localhost:8000/stock?page=2 500 (Internal Server Error)

不知道是什么问题。

这是我的控制器:

public function index()
{
    $menu = 'stock';
    $sub_menu = 'stock_list';
    $category = DB::table('categories')->select('cat_id','category_name')->get();
    $units = DB::table('units')->select('unit_id','unit_name')->get();
    $brands = DB::table('brands')->select('bid','brand_name')->get();
    $purchase_type = DB::table('purchase_types')->select('pt_id','type')->get();

    $items = DB::table('items')
        ->join('categories','categories.cat_id','=','items.category_id')
        ->join('units','units.unit_id','=','items.unit_id')
        ->select('items.*','categories.cat_id','categories.category_name','units.unit_id','units.unit_name')
        ->where('items.quantity','>',0)
        ->paginate(1);
    $total = Item::where('status', 0)->count();
    if (Request::ajax()) {
        return Response::json(view('partials.pagination')->with(compact('items','total'))->render());
    }
    return view('stock.stock-list', compact('items','category', 'purchase_type', 'units','brands','total','menu', 'sub_menu'));
}

还有js代码:

  $(document).ready(function (){
  $(window).on('hashchange',function(){
    page = window.location.hash.replace('#','');
    getItems(page);
 });
 $(document).on('click','.pagination a', function(e){
    e.preventDefault();
    var page = $(this).attr('href').split('page=')[1];
    // getItems(page);
    location.hash = page;
});
function getItems(page){
    $.ajax({
        url: '/stock?page=' + page
    }).done(function(data){
        $('#theContent').html(data);
    });
}

查看:

<div class="col-sm-6 text-right" id="pagination">
   {{ $items->links() }}

 </div>

【问题讨论】:

  • 它的 php 分页不是 ajax 分页
  • 而且你无法通过 ajax 维护分页
  • 我想你误会了,我的意思是要使用 ajax 加载下一页数据而不刷新页面,否则它可以正常工作。
  • 您是否启用了调试功能?xhr 主体是什么样的?
  • 您将视图编码为 json 大声笑?不错的一个

标签: jquery ajax pagination laravel-5.2


【解决方案1】:

返回一个不是 json 对象的视图

if (Request::ajax()) {
        return view('partials.pagination')->with(compact('items','total'))->render();
    }

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 2016-05-01
    • 2015-06-11
    • 2018-01-17
    • 2016-09-18
    • 2018-11-26
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多