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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?