简体   繁体   English

Laravel 5.8 ajaxupload POST 419(未知状态)

[英]Laravel 5.8 ajaxupload POST 419 (unknown status)

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). 我想使用ajaxupload上传img,但是我做不到,我总是收到异常POST 419(未知状态)。 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.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: 而我的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);
    }
});

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 这是一个通用脚本,应加载到任何DOM元素中

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

Note: Use correct name name="_token" or name="csrf-token" 注意:请使用正确的名称name =“ _ token”或name =“ csrf-token”

I had exactly same issue with Dropzone upload Please don't forget to add enctype="multipart/form-data" as form attribute And try to send this token data like that 我在Dropzone上传中遇到了完全相同的问题,请不要忘记添加enctype =“ multipart / form-data”作为表单属性,并尝试像这样发送此令牌数据

data: {
   _token: $('meta[name="csrf-token"]').attr('content'),
   name: buttonSingle.data('name')
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM