1

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);
    }
});
Sasha San
  • 211
  • 1
  • 2
  • 12
  • Chances are your csrf token isn't making it. Add the headers attribute to your AjaxUpload or send it with data: `data: {name: buttonSingle.data('name'), '_token' : "{{csrf_token()}}"},` – Adam Rodriguez Jun 17 '19 at 20:23
  • 3
    Possible duplicate of [Laravel 5.5 ajax call 419 (unknown status)](https://stackoverflow.com/questions/46466167/laravel-5-5-ajax-call-419-unknown-status) – Kate Orlova Jun 17 '19 at 20:27
  • I added the headers attribute to your AjaxUpload - no results. data: {name: buttonSingle.data('name'), '_token' : "{{csrf_token()}}"} - no results. – Sasha San Jun 17 '19 at 20:46
  • can anyone tell me , can I use AjaxUpload.js for Laravel , I think that it`s not working with Laravel and this is my problem. I mean this one https://gist.github.com/harpreetsi/3369391 – Sasha San Jun 17 '19 at 22:38

2 Answers2

0

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"

Khachik
  • 9
  • 3
  • I have already tried all the options. well all. I have no idea bro. – Sasha San Jun 18 '19 at 12:11
  • 1
    Hi, if you are using external plugin for file Upload, that will not work – Khachik Jun 18 '19 at 20:13
  • 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 ``` data: { _token: $('meta[name="_token"]').attr('content'), name: buttonSingle.data('name') }, ``` – Khachik Jun 18 '19 at 20:32
  • thx bro! I really had used all options and after your words : Hi, if you are using external plugin for file Upload, that will not work. I turn to another way. I used AjaxUpload.js external plugin and I told about it but nobody told me like you. I change plugin to usually $.ajax({}) and all is fine. So three day had gone to hell ))) Thx bro a lot ! Have a nice day ! – Sasha San Jun 19 '19 at 11:37
0

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

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