英文:
Ajax datatable using Laravel 10
问题
我缩短了代码,只保留了id和action两列。它应该使用ajax从销售表中获取所有数据,但当我运行网站时,弹出窗口显示jquery-3.5.1.js:10099 GET http://sample.test/sale.list?draw=1&columns%5B0%5D%5Bdata%5D...404 (Not Found)
,并显示DataTables warning: table id=dataTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7
警告。
<!-- 表格 -->
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JS
<script type="text/javascript">
$(function () {
var sale_url = "{{config('app.url')}}/list/"; // 绝对链接
var table = $('.table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('sale.list') }}",
columns: [
{ data: 'id', name: 'id' },
]
});
});
</script>
Controller
public function getSales(Request $request) {
if ($request->ajax()) {
$sales = Sale::orderby('id', 'asc')->select('*')->get();
return Datatables::of($sales)
->addIndexColumn()
->addColumn('action', function($row) {
$actionBtn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $actionBtn;
})
->rawColumns(['action'])
->make(true);
}
}
Route
Route::get('list', 'SaleController@getSales')->name('sale.list');
英文:
I shortened the code with columns id and action. It's supposed to get all the data from the sales table using ajax but when I run the site jquery-3.5.1.js:10099 GET http://sample.test/sale.list?draw=1&columns%5B0%5D%5Bdata%5D...404 (Not Found)
shows up with pop up DataTables warning: table id=dataTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7
warning
<!-- table -->
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JS
<script type="text/javascript">
$(function () {
var sale_url = "{{config('app.url')}}/list/"; // absolute link
var table = $('.table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('sale.list') }}",
columns: [
{ data: 'id', name: 'id' },
]
});
});
</script>
Controller
public function getSales(Request $request) {
if ($request->ajax()) {
$sales = Sale::orderby('id', 'asc')->select('*')->get();
return Datatables::of($sales)
->addIndexColumn()
->addColumn('action', function($row) {
$actionBtn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
return $actionBtn;
})
->rawColumns(['action'])
->make(true);
}
}
Route
Route::get('list', 'SaleController@getSales')->name('sale.list');
答案1
得分: 1
将 url()
替换为 route()
,你就可以正常运行了。如果你想使用 url()
,那么你需要像这样使用:url("/list/")
,或者如果你在这个路由之上使用了任何 prefix
,比如 sale
,那么应该是 url("/sale/list")
。
这里 url() 将使用路由的路径:
url("/list/")
而 route() 将使用路由的名称:
route('sale.list')
你的 ajax 函数应该像这样:
<script type="text/javascript">
$(function () {
var table = $('.table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('sale.list') }}",
columns: [
{ data: 'id', name: 'id' },
]
});
});
</script>
英文:
Replace url()
with route()
and you will be good to go, if you wish to use url() then you've to use like url("/list/")
or you if you're using any group prefix
above this route like sale
then it would be url("/sale/list")
Here url() will use the path of route
url("/list/")
And route() will use name of route
route('sale.list')
Your ajax function would be like this,
<script type="text/javascript">
$(function () {
var table = $('.table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('sale.list') }}",
columns: [
{ data: 'id', name: 'id' },
]
});
});
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论