【问题标题】:The PUT method is not supported for this route. Supported methods: POST using laravel ajax此路由不支持 PUT 方法。支持的方法:使用 laravel ajax 的 POST
【发布时间】:2020-12-05 13:54:41
【问题描述】:

我在 annonces 表中有多个图像,我想更新多个图像,我只想将数据发送到控制器,但它给了我错误:The PUT method is not supported for this route. Supported methods: POST。我不知道错误在哪里。

web.php

Route::post('filesUpdate','AnnoncesController@filesUpdate')->name('filesUpdate');

AnnoncesController.php

public function filesUpdate(Request $request)
    {
      dd($request->all());
    }

details.blade.php

<form method="post" action="{{route('filesUpdate')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">
     <input type="hidden" name="_method" value="PUT">
        {{ csrf_field() }}                            
</form>  
<script type="text/javascript">
            Dropzone.options.dropzone = 
            {
            maxFilesize: 12,
            renameFile: function(file) {
                var images = [];
                $(file).each(function(i){
                  images[i] = file.name;
                  console.log(images);
                }); 
                $.ajax({
                 url:"{{ route('filesUpdate') }}" ,
                 type:'POST',
                 data:{
                    images  : images,
                    "_token": "{{ csrf_token() }}"
                },
                 success: function(data, status, xhr){
                  console.log(data, status, xhr);
                },
                error: function(xhr, status, msg) {
                  return console.error(xhr, status, msg)
                }})
               //return  images;
            }, 
            acceptedFiles: ".jpeg,.jpg,.png,.gif",
            addRemoveLinks: true,
            timeout: 50000,
            success: function(file, response) 
            {
                console.log(response);
            },
            error: function(file, response)
            {
               return false;
            }
            };
  </script>

【问题讨论】:

    标签: php ajax laravel dropzone


    【解决方案1】:

    &lt;input type="hidden" name="_method" value="PUT"&gt; 负责方法欺骗 - 意味着它指示 Laravel 使用 PUT 方法来处理请求。

    由于您的路线被定义为 POST

    Route::post('filesUpdate','AnnoncesController@filesUpdate')->name('filesUpdate');
    

    尝试从表单中删除 &lt;input type="hidden" name="_method" value="PUT"&gt; 以消除错误 - 如果它有效,则意味着您的表单没有通过 ajax 提交

    【讨论】:

    • 谢谢你回答它给我错误消息"Unauthenticated."
    • 如果 auth 中间件应用于包含路由的路由组或控制器的 __construct 则需要登录用户才能发出发布请求
    【解决方案2】:

    来自laravel docs

    Route::match(['put', 'post'], '/', function () {
        //
    });
    

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 2020-05-27
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多