Laravel 和 Datatable 服务器端

问题描述 投票:0回答:1

我的基于 Laravel 10 的项目中的 Datatables 库有问题。

也许有人遇到过类似的问题并且可以提供帮助。

一切在本地都适用,但在生产服务器上不起作用。

当我使用服务器端处理器时,我收到错误代码 403 禁止

Laravel 10

下面是我的代码

控制器

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
use Yajra\DataTables\Facades\DataTables;

class ClientController extends Controller
{
    public function index(Request $request)
    {
        
        if ($request->ajax()) {
            $query = Client::select(sprintf('%s.*', (new Client)->table));
            $table = Datatables::of($query);

            $table->addColumn('placeholder', '&nbsp;');
            $table->addColumn('actions', '&nbsp;');

            $table->editColumn('id', function ($row) {
                return $row->id ? $row->id : "";
            });
            $table->editColumn('registration_plate', function ($row) {
                return $row->registration_plate ? $row->registration_plate: "";
            });
            $table->editColumn('driver', function ($row) {
                return $row->driver ? $row->driver : "";
            });

            $table->rawColumns(['actions', 'placeholder']);

            return $table->make(true)->toJson();
        }

        return view('admin.clients.index');
    }

我的看法

@extends('layouts.admin')
@section('content')
<div class="card">
    <div class="card-header">
        Drivers
    </div>

    <div class="card-body">
        <table class=" table table-bordered table-striped table-hover ajaxTable datatable datatable-Client">
            <thead>
                <tr>
                    <th width="10">

                    </th>
                    <th>
                       ID
                    </th>
                    <th>
                        Registration PLate
                    </th>
                    <th>
                       Driver
                    </th>
                    <th>
                        &nbsp;
                    </th>
                </tr>
            </thead>
        </table>
    </div>
</div>



@endsection
@section('scripts')
@parent
<script>
    $(function () {
  let dtButtons = $.extend(true, [], $.fn.dataTable.defaults.buttons)
@can('client_delete')
  let deleteButtonTrans = '{{ trans('global.datatables.delete') }}';
  let deleteButton = {
    text: deleteButtonTrans,
    url: "{{ route('admin.clients.massDestroy') }}",
    className: 'btn-danger',
    action: function (e, dt, node, config) {
      var ids = $.map(dt.rows({ selected: true }).data(), function (entry) {
          return entry.id
      });

      if (ids.length === 0) {
        alert('{{ trans('global.datatables.zero_selected') }}')

        return
      }

      if (confirm('{{ trans('global.areYouSure') }}')) {
        $.ajax({
          headers: {'x-csrf-token': _token},
          method: 'POST',
          url: config.url,
          data: { ids: ids, _method: 'DELETE' }})
          .done(function () { location.reload() })
      }
    }
  }
  dtButtons.push(deleteButton)
@endcan

  let dtOverrideGlobals = {
    buttons: dtButtons,
    processing: true,
    serverSide: true,
    retrieve: true,
    aaSorting: [],
    ajax: "{{ route('admin.clients.index') }}",
    columns: [
      { data: 'placeholder', name: 'placeholder' },
{ data: 'id', name: 'id' },
{ data: 'registration_plate', name: 'registration_plate' },
{ data: 'driver', name: 'driver' },
{ data: 'actions', name: '{{ trans('global.actions') }}' }
    ],
    orderCellsTop: true,
    order: [[ 1, 'asc' ]],
    pageLength: 50,
  };
  let table = $('.datatable-Client').DataTable(dtOverrideGlobals);
  $('a[data-toggle="tab"]').on('shown.bs.tab click', function(e){
      $($.fn.dataTable.tables(true)).DataTable()
          .columns.adjust();
  });
  
});

</script>
@endsection

Datatables 打印警报“DataTables 警告:表 id=DataTables_Table_0 - Ajax 错误。有关此错误的更多信息,请参阅 https://datatables.net/tn/7”

我做错了什么?哪里可以找到问题的解决方案?

datatables
1个回答
0
投票

我解决了这个问题。我给出解决方案,也许对某人有用。我修改了视图文件,将负责 URL 的代码片段更改为以下代码:

ajax: {
    url: "{{ route('admin.clients.list')}}",
    type: 'post',
    headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
},
© www.soinside.com 2019 - 2024. All rights reserved.