Ajax数据表格使用Laravel 10

huangapple go评论71阅读模式
英文:

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&amp;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

&lt;!-- table --&gt;
&lt;table class=&quot;table&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;ID&lt;/th&gt;
            &lt;th&gt;Action&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

JS

&lt;script type=&quot;text/javascript&quot;&gt;
    $(function () {
        var sale_url = &quot;{{config(&#39;app.url&#39;)}}/list/&quot;; // absolute link
        var table = $(&#39;.table&#39;).DataTable({
            processing: true,
            serverSide: true,
            ajax: &quot;{{ url(&#39;sale.list&#39;) }}&quot;,
            columns: [
                { data: &#39;id&#39;, name: &#39;id&#39; },
            ]
        });
    });
&lt;/script&gt;

Controller

public function getSales(Request $request) {
    if ($request-&gt;ajax()) {
        $sales = Sale::orderby(&#39;id&#39;, &#39;asc&#39;)-&gt;select(&#39;*&#39;)-&gt;get();

        return Datatables::of($sales)
            -&gt;addIndexColumn()
            -&gt;addColumn(&#39;action&#39;, function($row) {
                $actionBtn = &#39;&lt;a href=&quot;javascript:void(0)&quot; class=&quot;edit btn btn-success btn-sm&quot;&gt;Edit&lt;/a&gt; &lt;a href=&quot;javascript:void(0)&quot; class=&quot;delete btn btn-danger btn-sm&quot;&gt;Delete&lt;/a&gt;&#39;;
                return $actionBtn;
            })
            -&gt;rawColumns([&#39;action&#39;])
            -&gt;make(true);
    }
}

Route

Route::get(&#39;list&#39;, &#39;SaleController@getSales&#39;)-&gt;name(&#39;sale.list&#39;);

答案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(&quot;/list/&quot;) or you if you're using any group prefix above this route like sale then it would be url(&quot;/sale/list&quot;)

Here url() will use the path of route

url(&quot;/list/&quot;)

And route() will use name of route

route(&#39;sale.list&#39;)

Your ajax function would be like this,

&lt;script type=&quot;text/javascript&quot;&gt;
    $(function () {
        var table = $(&#39;.table&#39;).DataTable({
            processing: true,
            serverSide: true,
            ajax: &quot;{{ route(&#39;sale.list&#39;) }}&quot;,
            columns: [
                { data: &#39;id&#39;, name: &#39;id&#39; },
            ]
        });
    });
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年6月2日 12:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定