【发布时间】:2019-11-06 10:14:42
【问题描述】:
我刚刚开始学习 ajax laravel,我只是按照教程学习,然后它对我不起作用。谢谢
我的 ajax 看起来像这样:
$(document).ready(function(){
$('#courseTable').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{{ route('courses.index') }}}",
},
然后在路线中:
Route::resource('courses', 'CourseController');
那么对于我的课程控制器:
public function index()
{
// $courses = Course::orderBy('description','asc')->paginate(8);
if (request()->ajax()) {
return datatables()->of(Course::latest()->get())
->addColumn('action', function($data){
$button = '<button type="button" name="edit" id="'.$data->courseID.'"
class="edit btn btn-warning btn-sm">Edit</button>';
$button .= ' ';
$button .= '<button type="button" name="delete" id="'.$data->courseID.'"
class="delete btn btn-warning btn-sm">Delete</button>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('registrar.courses.index');
// ->with('courses', $courses)
}
此错误显示如下:
app.js:16034 GET http://odrs.test/%7B%7B%7B%20route('courses.index')%20%7D%7D%7D?draw=1&colum 404(未找到)
【问题讨论】:
-
您的 ajax 代码是否在
.blade.php文件中? -
不,先生。它在一个单独的文件中,然后在我的 Blade.php 文件中调用它
-
如果它不在
.blade.php文件中,则不能使用刀片语法({{ }}、{!! !!}等)您必须找到另一种方法来定义该路由并在那里使用它。也许在包含 JS 之前将其定义为.blade.php文件中的变量?