簡體   English   中英

無法從Laravel中的javascript調用控制器內部的函數

[英]Cannot call the function inside a controller from the javascript in laravel

我正在嘗試使用jquery的數據表創建具有從數據庫中提取的成員的表。 這是我的html和javascript代碼:

<table id="workerTable" class="table-bordered table-hover" width="80%" cellspacing="0">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Role</th>
                <th>Dep_id</th>
                <th>Start_Date</th>
                <th>Updated</th>
            </tr>
        </thead>
    </table>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#workerTable').DataTable( {
                "processing": true,
                "serverSide": true,
                "ajax": {{ URL::route('workerData') }}
            } );
        } );
    </script> 

我定義了以下路線:

Route::get('/workers/data' , 'WorkersController@fetch')->name('workerData');

而且WorkersController內部的函數fetch()就像這樣:

public function fetch()
    {
        $workers = Worker::all();
        echo json_encode($workers);
    }

我是laravel的新手,我認為我不太了解。 是這條線的電話嗎

“ ajax”:{{URL :: route('workerData')}}

使路由調用WorkersController的fetch函數?

如果尚未使用此軟件包,則應該使用它: https : //github.com/yajra/laravel-datatables

然后更換
"ajax": {{ URL::route('workerData') }}
通過
"ajax": {{ route('workerData') }}

這是您功能的更正

use App\Worker;
use Yajra\Datatables\Datatables;

// ...

public function fetch()
{
    $workers = Worker::all();

    return Datatables::of($workers)->make(true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM