簡體   English   中英

Laravel 5.8 ajaxupload POST 419(未知狀態)

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

我有個問題。 我想使用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);
    }
});

您應該在標題部分中添加類似內容

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

要么

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

這是一個通用腳本,應加載到任何DOM元素中

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

注意:請使用正確的名稱name =“ _ token”或name =“ csrf-token”

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