dongzhang7157 2016-12-07 14:51
浏览 107
已采纳

Laravel - 使用ajax将2个变量从视图传递到控制器

I have this function for my delete button

<a href="#" onclick='deleteFile("{{ ($event->id) }}", "{{ ($file->name) }}")' class="btn btn-danger btn-xs delete">L&ouml;schen</a>

and the route in routes/web.php

Route::post('/delete-file', 'MyController@deleteEventFile');

that hits this function

public function deleteEventFile($eventid, $filename){
   dd($eventid);
}

and this is my ajax function:

function deleteFile(eventid, filename){
    //alert(filename);
    //alert(eventid);
    $.ajax({
      url: '/delete-file/',
      type: "post",
      data:{ _token: "{{csrf_token()}}", eventid: eventid, filename: filename },
      dataType: 'json',
    });
}

And i always get this error:

Missing argument 1 for App\Http\Controllers\MyController::deleteEventFile()

my variables can't get through... How to pass the eventid and filename to controller

  • 写回答

1条回答 默认 最新

  • doucaishou0074 2016-12-07 14:57
    关注

    According to your code, you are expecting route to give 2 params - eventid & filename into the controller method.

    Instead it you should code your method like this:

    public function deleteEventFile() {
       $event_id = request()->get('eventid');
       $file_name = request()->get('filename');
    }
    

    Fetch the POST data from the laravel's request() method instead.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog