我正在将ajax请求与数据发送到从数据库返回数据的方法中。我确实得到了响应(在网络下检查),但是由于某种原因它没有被插入表中。这是我的代码:
<table class="table table-hover" id="users-table">
<thead>
<tr>
<th>Id</th>
<th>Id_sport</th>
<th>Id_league</th>
<th>Id_user</th>
<th>Points</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
</thead>
</table>
<script>
$(function() {
$('#users-table').DataTable({
processing: true,
serverSide: true,
"ajax": {
url: "{!! route('get.leaderboardsData') !!}",
dataSrc: "",
data: { id_sport: "3" }
},
columns: [
{ data: 'id', name: 'id' },
{ data: 'id_sport', name: 'id_sport' },
{ data: 'id_league', name: 'id_league' },
{ data: 'id_user', name: 'id_user' },
{ data: 'points', name: 'points' },
{ data: 'created_at', name: 'created_at' },
{ data: 'updated_at', name: 'updated_at' }
]
});
});
</script>
这是从数据库返回数据的函数:
public function leaderboardsData(Request $request)
{
$sport_id = $request->get('id_sport');
if($sport_id==="0")
{
$query = DB::table('scores');
return DataTables::of($query)->toJson();
}
else
{
$query = DB::table('scores')->where('id_sport', '=',$sport_id);
return DataTables::of($query)->toJson();
}
}
我正在Laravel开发。