donglian5309 2018-11-21 08:31
浏览 54
已采纳

使用laravel中的ajax从按钮上单击文件夹中删除图像

I am trying to delete image from folder using ajax and in route using delete method .In controller trying to delete image using image name in laravel.

Route:

Route::delete('remove-social/{filename}', 'Webadmin\Socials@removesocial');

Controller:

 public function removesocial($filename){
          File::delete('public/assets/uploads/Social/' . $filename);
   }

View :

 <a href="javascript:removesocialimage()" style="color: white;text-decoration: none;" class="btn btn-red">
                                <i class="glyphicon glyphicon-trash "></i> Remove</a>   </label>
    <script>
         function removesocialimage() {
                if (j('#file_name').val() != '')
                    if (confirm('Are you sure want to remove social icon?')) {
                        j('#loading').css('display', 'block');
                        var form_data = new FormData();
                        form_data.append('_method', 'DELETE');
                        form_data.append('_token', '{{csrf_token()}}');
                        j.ajax({
                            url: "remove-social/" + j('#file_name').val(),
                            data: form_data,
                            type: 'POST',
                            contentType: false,
                            processData: false,
                            success: function (data) {
                              j('#preview_image').attr('src', '{{URL::to('/public/assets/Webadmin/images/attach-1.png')}}');
                                j('#file_name').val('');
                                j('#loading').css('display', 'none');
                            },
                            error: function (xhr, status, error) {
                                alert(error);
                                alert(xhr.responseText);
                            }
                        });
                    }
            }
        </script>    
  • 写回答

2条回答 默认 最新

  • dongyejun1983 2018-11-21 08:35
    关注

    Include this in your views head:

    <meta name="csrf-token" content="{{ csrf_token() }}">
    

    And do this ajax setup before making network calls:

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器