【问题标题】:Is there other way to set url using route?还有其他方法可以使用路由设置网址吗?
【发布时间】: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 .= '&nbsp;&nbsp;';
                $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 文件中的变量?

标签: ajax laravel routes


【解决方案1】:

不可能在单独的 javascript 文件中编写刀片模板。

您可能有两种方法:

  1. 小时候加入刀片视图中的 eventListener。

  2. 将你的 JS 文件转移到你的视图文件中,将其扩展名更改为 Blade.php 并将其包含在脚本标签之间的视图中:

<script>
@include("my-view-js")
</script>

【讨论】:

  • 我尝试了第二个选项如何保存 my-view-js.blade.php。它返回 $ 未定义错误?
  • 你得到了错误,因为你没有在你的视图文件中包含 jquery
  • 谢谢先生。即使数据表变形,它现在也可以工作
猜你喜欢
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2017-04-17
  • 2018-12-17
  • 2011-10-19
相关资源
最近更新 更多