【问题标题】:The POST method is not supported for this route. Supported methods: GET, HEAD, PUT此路由不支持 POST 方法。支持的方法:GET、HEAD、PUT
【发布时间】:2020-05-27 11:33:20
【问题描述】:

我有一个带复选框的薪水数据表和一个由 CHANTIERS 填写的选择,我想根据检查线更新 select 中选择的 CHANTIER 的多个 SALARIES 的 CHANTIERS,但它给了我错误。

嗨,我有一个带复选框的薪水数据表和一个由 CHANTIERS 填写的选择,我想根据检查线更新 select 中选择的 CHANTIER 的多个薪水的 CHANTIERS,但它给了我错误.

affectation.blade.php

 <div class="form-group col-md-3">
                  <select class="form-control" id="chantier">
                          <option></option>
                           @foreach($chantiers as $chantier)
                          <option value="{{ $chantier->id }}">{{ $chantier->chantier}}</option>
                           @endforeach
                  </select>
              </div>
              <div class="form-group col-md-4">
               <button class="btn btn-theme update-all" data-url="">Update All</button>
              </div>
<table id="example" class="table table-striped table-bordered" style="width:100%">
                 <thead>
                  <tr>
                    <th><input type="checkbox" id="check_all"></th>
                    <th>nom prenom</th>
                    <th>cin</th>
                    <th>matricule</th>
                    <th>chantier</th>
                  </tr>
                 </thead>
                <tbody>
                  @foreach($salaries as $salarie)
                  <tr id="{{$salarie->id}}">
                    <td><input type="checkbox" class="checkbox" name="customer_id[]" value="{{$salarie->id}}" /></td>
                    <td>{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                    <td>{{ $salarie->cin }}</td>
                    <td>{{ $salarie->matricule }}</td>
                    <td>{{ $salarie->chantier->chantier }}</td>
                  </tr>
                  @endforeach
                </tbody>
</table>

jQuery

   <script type="text/javascript">
    $(document).ready(function () {
        $('#check_all').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".checkbox").prop('checked', true);  
         } else {  
            $(".checkbox").prop('checked',false);  
         }  
        });
         $('.checkbox').on('click',function(){
            if($('.checkbox:checked').length == $('.checkbox').length){
                $('#check_all').prop('checked',true);
            }else{
                $('#check_all').prop('checked',false);
            }
         });
        $('.update-all').on('click', function(e) {

          if(confirm("Are you sure you want to update this?"))
  {
   var id = [];
   var chantier;

   $(':checkbox:checked').each(function(i){
    id[i] = $(this).val();
   });

   if(id.length === 0){
    alert("Please Select atleast one checkbox");
   }else{
    let chantier = $('#chantier').val();
    $.ajax({
     url:"{{ route('salarie.multiple-update') }}",
     method:'PUT',
     headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
     data:{
           id:id,
           chantier:chantier
    },
     success:function(){
      for(var i=0; i<id.length; i++)
      {
      $('tr#'+id[i]).find('td:last-child').html(chantier);
      }
     }

    });
   } 
  }
  else
  {
   return false;
  }

    });
    });
</script>

web.php

Route::PUT('affectation', ['as'=>'salarie.multiple-update','uses'=>'SalarieController@updateMultiple']); 

SalrieController.php

public function updateMultiple(Request $request){
     Salarie::find(explode(',',request('ids')))->each(function($item) {
      $item->update(['chantier_id' => request('chantier_id')]);
     });
     return response()->json(['status'=>true]); 
    }

【问题讨论】:

  • 您尝试在 URL 上添加 _method:'put'

标签: php jquery laravel


【解决方案1】:

HTTP 不支持PUT 方法,您可以使用 POST 并将 _method=PUT 参数添加到数据正文中。即:

<script type="text/javascript"> $.ajax({ url:"{{ route('salarie.multiple-update') }}", method:'POST', headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, data:{ id:id, chantier:chantier, _method: "PUT" }); </script>

【讨论】:

    【解决方案2】:

    PHP 本身不支持 PUT HTTP 方法。为此,您可以使用 POST 并将 _method=PUT 参数添加到 queryString 中。即:

     <script type="text/javascript">
        $.ajax({
         url:"{{ route('salarie.multiple-update') }}" + '?_method=PUT',
         method:'POST',
         headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
         data:{
           id:id,
           chantier:chantier
        });
    </script>
    
    

    【讨论】:

    • 输出是什么?
    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2020-04-03
    • 2020-02-16
    • 1970-01-01
    • 2019-08-28
    相关资源
    最近更新 更多