Laravel 5.8 ajaxupload POST 419(状态未知)

我有个问题。我想使用 ajaxupload 上传 img 但我做不到,我总是收到异常 POST 419(未知状态)。我做了所有使用文档的事情,但我不知道。


所以,我的路线:


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

在主要布局中,我有:


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

我的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>

还有我的 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);

    }

});


梵蒂冈之花
浏览 154回答 2
2回答

波斯汪

我在 Dropzone 上传时遇到了完全相同的问题请不要忘记添加 enctype="multipart/form-data" 作为表单属性并尝试发送这样的令牌数据data: {&nbsp; &nbsp;_token: $('meta[name="csrf-token"]').attr('content'),&nbsp; &nbsp;name: buttonSingle.data('name')},

阿波罗的战车

你应该在标题部分有类似的东西<meta name="_token" content="{{ csrf_token() }}"><meta>或者<meta name="csrf-token" content="{{ csrf_token() }}">这是一个应该在任何 DOM 元素中加载的通用脚本<script>&nbsp; &nbsp; $(function () {&nbsp; &nbsp; &nbsp; &nbsp; $.ajaxSetup({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>注意:使用正确的名称 name="_token" 或 name="csrf-token"
打开App,查看更多内容
随时随地看视频慕课网APP