我收到DataTables警告:table id = DataTables_Table_0-Ajax错误。多数情况下会返回正确的数据,但是每三到四次调用一次,就会出现此警告,并且数据表中未加载任何数据。有人可以识别我的代码有什么问题吗?我是Web开发的初学者。
在调试时,我发现出现此警告时未发送对控制器的调用。控制台显示“ 404 Not found”。我认为这是找不到的路线。但是,为什么它大多数时候都在加载。 我还在控制器中放置了一个Info($ request),出现此错误时Laravel日志为空。
我正在使用Laravel 5.8。 HTML代码是:
<div class="box-body">
<table class="data-table display compact">
<thead>
<tr>
<th width="50px">Action</th>
<th>Date</th>
<th>Ref. No.</th>
<th>Supplier</th>
<th>Inv. No.</th>
<th>Inv. Date</th>
<th>Total</th>
<th>Freight</th>
<th>Discount</th>
<th>Cash</th>
<th>Net Total</th>
<th>Remarks</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
Ajax代码:
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('purchlist') }}",
columns: [
{data: 'action', name: 'action', orderable: false, searchable: false, width: "50px"},
{data: 'date', name: 'date'},
{data: 'refno', name: 'refno'},
{data: 'title', name: 'title'},
{data: 'sup_invno', name: 'sup_invno'},
{data: 'sup_invdate', name: 'sup_invdate'},
{data: 'total', name: 'total'},
{data: 'freight', name: 'freight'},
{data: 'discount', name: 'discount'},
{data: 'cash', name: 'cash'},
{data: 'nettotal', name: 'nettotal'},
{data: 'remarks', name: 'remarks'},
{data: 'status', name: 'status'},
]
});
和控制器代码:
public function index(Request $request)
{
$branch_id=session()->get('lbranch','0');
if ($branch_id==0){
return redirect()->route('login');
}
if ($request->ajax()) {
$data = DB::table('purchases')
->where('purchases.branchid',$branch_id)
->where('accounts.branchid',$branch_id)
->leftjoin('accounts','purchases.account_code','=','accounts.code')
->select('purchases.*',
DB::raw('(CASE
WHEN purchases.posted = "1" THEN "Posted"
ELSE "Unposted"
END) AS status'),
'accounts.title')
->latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" class="edit btn btn-primary btn-sm editPurchase"><i class="fa fa-edit"></i></a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" class="btn btn-danger btn-sm deletePurchase"><i class="fa fa-trash"></i></a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('purchases',compact('purchases'));
}