首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >此路由不支持POST方法。支持的方法: GET、HEAD、PUT

此路由不支持POST方法。支持的方法: GET、HEAD、PUT
EN

Stack Overflow用户
提问于 2020-02-12 00:29:22
回答 2查看 98关注 0票数 1

我有一个带有复选框的薪给数据表和一个由CHANTIERS填写的选择,我想要更新selected中选择的薪酬的多个CHANTIER,但是它会给我带来错误。

嗨,我有一个带有复选框的薪给数据表和一个由CHANTIERS填写的选择,我想要按选中的CHANTIER更新多个薪酬的CHANTIER,根据选中的行,但是它会给我带来错误。

affectation.blade.php

代码语言:javascript
复制
 <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

代码语言:javascript
复制
   <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

代码语言:javascript
复制
Route::PUT('affectation', ['as'=>'salarie.multiple-update','uses'=>'SalarieController@updateMultiple']); 

SalarieController.php

代码语言:javascript
复制
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]); 
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-12 13:06:45

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

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

票数 0
EN

Stack Overflow用户

发布于 2020-02-12 03:31:39

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

代码语言:javascript
复制
 <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>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60179371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档