dream04110 2019-06-17 20:12
浏览 232

Laravel 5.8 ajaxupload POST 419(未知状态)

I have a problem. I want to upload img using ajaxupload but i can`t do it, i always get exception POST 419 (unknown status). I do everything using to the documentation but I have no idea.

So, my route:

Route::post('/products/image','ProductController@image');

In main layouts I have:

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

My form.blade.php

 <form action="{{route('')}}" method="post">
 @csrf
 <div class="box box-danger box-solid file-upload">
    <div class="box-body">
       <div id="single" class="btn btn-success" 
        data-url="products/image" data-name="single">
           Chose
        </div>
        <div class="single"></div>

And my app.js:

  $.ajaxSetup({
      headers: {
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
     }
  });


  if($('div').is('#single')){
        var buttonSingle = $("#single"),
        buttonMulti = $("#multi"),
        file;
    }

   if(buttonSingle){
     new AjaxUpload(buttonSingle, {
     action: '/admin/' + buttonSingle.data('url') + "?upload=1",
     data: {name: buttonSingle.data('name')},
     name: buttonSingle.data('name'),

     onSubmit: function(file, ext){
        if (! (ext && /^(jpg|png|jpeg|gif)$/i.test(ext))){
            alert('Exception');
            return false;
        }

      buttonSingle.closest('.file-upload').find('.overlay').css({'display':'block'});

    },

     onComplete: function(file, response){
        $res = JSON.parse(response);
        if($res['error']){
            alert($res['error']);
            buttonSingle.closest('.file-upload').find('.overlay').css({'display': 'none'});
            return false;
        }
        setTimeout(function(){
            buttonSingle.closest('.file-upload').find('.overlay').css({'display':'none'});

            response = JSON.parse(response);
            $('.' + buttonSingle.data('name')).html('<img src="/images/' + response.file + '" style="max-height: 150px;">');
        }, 1000);
    }
});
  • 写回答

2条回答 默认 最新

  • duanli9001 2019-06-17 23:53
    关注

    You should have something like that in header section

    <meta name="_token" content="{{ csrf_token() }}"><meta>
    

    or

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

    And this is a common script which should load in any DOM element

    <script>
        $(function () {
            $.ajaxSetup({
                headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
            });
        });
    </script>
    

    Note: Use correct name name="_token" or name="csrf-token"

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退