When I create an article, upload an image, everything works fine and is created. But when editing an article, an empty array is sent to the server. Why? Here is the code that I use to add an article.
$('#form').on('submit', function(e) { e.preventDefault(); var form = $('#form'); var formData = new FormData($(this)[0]); $.ajax({ url : form.attr("action"), type : form.attr("method"), data : formData, dataType: 'json', cache: false, contentType: false, processData: false, headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }, error :function( errors ) { output = "<div class='alert alert-danger'><h4><i class='icon fa fa-ban'></i> Возникла ошибка!</h4><ul>"; $.each(errors.responseJSON, function(index, error){ output += "<li>" + error + "</li>"; }); output += "</ul></div>"; $('#append').html(output); }, success: function(data){ output = "<div class='alert alert-success'><h4><i class='icon fa fa-check'></i> Готово!</h4><ul>"; output += data.success; output += "</ul></div>"; $('#append').html(output); setTimeout(function(){ window.location.replace('{{ url('panel/catalog') }}')}, 3500 ); } }) }) Here is the code when editing the article. All the same, only the method changes
$('#form').on('submit', function(e) { e.preventDefault(); var form = $('#form'); var formData = new FormData($(this)[0]); $.ajax({ url : form.attr("action"), type : 'patch', data : formData, dataType: 'json', cache: false, contentType: false, processData: false, headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }, error :function( errors ) { output = "<div class='alert alert-danger'><h4><i class='icon fa fa-ban'></i> Возникла ошибка!</h4><ul>"; $.each(errors.responseJSON, function(index, error){ output += "<li>" + error + "</li>"; }); output += "</ul></div>"; $('#append').html(output); }, success: function(data){ output = "<div class='alert alert-success'><h4><i class='icon fa fa-check'></i> Готово!</h4><ul>"; output += data.success; output += "</ul></div>"; $('#append').html(output); setTimeout(function(){ window.location.replace('{{ url('panel/catalog') }}')}, 3500 ); } }) })
FormDatacannot be sent in thepatchmethod, or just reading it on the server in a wrong way - Grundy_method. most likely suitable forPATCH- Grundyx-www-form-urlencodedType - Grundy