Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从laravel中的javascript调用控制器内的函数_Javascript_Jquery_Ajax_Laravel - Fatal编程技术网

无法从laravel中的javascript调用控制器内的函数

无法从laravel中的javascript调用控制器内的函数,javascript,jquery,ajax,laravel,Javascript,Jquery,Ajax,Laravel,我试图使用jquery的datatables创建一个包含从数据库中提取的成员的表。 这是我的html和javascript代码: <table id="workerTable" class="table-bordered table-hover" width="80%" cellspacing="0"> <thead> <tr> <th>Id</th>

我试图使用jquery的datatables创建一个包含从数据库中提取的成员的表。 这是我的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> 
WorkersController中的函数fetch()如下所示:

public function fetch()
    {
        $workers = Worker::all();
        echo json_encode($workers);
    }
我是拉雷维尔的新手,我想我对它不是很了解。这是打这条线的电话吗

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


是否创建调用WorkersController的fetch函数的路由

如果尚未使用此软件包,则应使用此软件包:

然后更换
“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);
}

假设输出是一个字符串,您可能需要在其周围加引号:
“ajax”:“{URL::route('workerData')}}”
我这样做了,但仍然没有做任何更改。
在fetch方法中返回$workers->toJson()
我照您说的做了,但是ajax返回html页面作为响应,而不是数据库对象。尝试使用此示例,它应该可以工作:
use App\Worker;
use Yajra\Datatables\Datatables;

// ...

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

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