2017-09-26 83 views
0

您好我有这个删除按钮,我真的想通过使用Ajax调用。我的问题是,当点击删除按钮,这是我得到的错误 NetworkError: 405 Method Not Allowed(1/1) MethodNotAllowedHttpException。这是下面405方法不允许 - (1/1)Laravel 5中的MethodNotAllowedHttpException

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('.delete-block').on("click","a#deleteBtn", function() { 
      var x = confirm("Do you want to delete this?"); 
      if(x){ 
       var id = $(this).data("deleteid"); 
       $.ajax({ 
        type:"post", 
        url: $(this).data("href"), 
        data: { 
         id: id 
        }, 
        success: function(data){ 
         $("#deleteId"+ id).fadeOut('slow'); 
        } 
       }); 
       return true; 
      }else{ 
       return false; 
      } 

      return false; 
     }); 
    }); 
</script> 

<tbody> 
    @foreach($datas as $post) 
    <tr class="delete-block" id="deleteId{{ $post['id'] }}"> 
     <td>{{ $post['title']}}</td> 
     <td>{{ $post['post'] }} </td> 
     <td>{{ $post['comments'] }}</td> 
     <td> 
      <a href="{{ url('home/edit', $post['id']) }}" class="btn btn-warning">Edit</a> 
      <a href="#" id="deleteBtn" data-deleteid="{{ $post['id'] }}" data-href="{{ action('[email protected]', $post['id']) }}" class="btn btn-danger">Delete</a> 
     </td> 
    </tr> 
    @endforeach 
</tbody> 

林我的代码使用action('[email protected]', $post['id'])因为正在生成我AddRecordController。而我的路由仅低于

public function destroy($id){ 
     // 
     echo "Test"; 
     echo $id; 
     $addRecord = Addrecord::find($id); 
     $addRecord->delete(); 
    } 

这个

Route::resource('addRecord', 'AddRecordController'); 

我AddRecordController代码有人可以帮助我想通了这事了呢?任何帮助,非常感激。 TIA。

+0

您需要使用''DELETE''方法。您可以使用[form method spoofing](https://laravel.com/docs/5.5/routing#form-method-spoofing)来执行此操作。 – ub3rst4r

+0

你的意思是删除数据时的表单方法?没有使用javascropt/ajax调用? –

+0

您可以使用AJAX。你只需要在数据中加入''“_method”:“DELETE”,''。 – ub3rst4r

回答

0

您需要使用DELETE方法。您可以使用form method spoofing来执行此操作。

试试这个:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('.delete-block').on("click","a#deleteBtn", function() { 
      var x = confirm("Do you want to delete this?"); 
      if(x){ 
       var id = $(this).data("deleteid"); 
       $.ajax({ 
        type:"post", 
        url: $(this).data("href"), 
        data: { 
         id: id, 
         "_method": "delete" 
        }, 
        success: function(data){ 
         $("#deleteId"+ id).fadeOut('slow'); 
        } 
       }); 
       return true; 
      }else{ 
       return false; 
      } 

      return false; 
     }); 
    }); 
</script> 

<tbody> 
    @foreach($datas as $post) 
    <tr class="delete-block" id="deleteId{{ $post['id'] }}"> 
     <td>{{ $post['title']}}</td> 
     <td>{{ $post['post'] }} </td> 
     <td>{{ $post['comments'] }}</td> 
     <td> 
      <a href="{{ url('home/edit', $post['id']) }}" class="btn btn-warning">Edit</a> 
      <a href="#" id="deleteBtn" data-deleteid="{{ $post['id'] }}" data-href="{{ action('[email protected]', $post['id']) }}" class="btn btn-danger">Delete</a> 
     </td> 
    </tr> 
    @endforeach 
</tbody> 
+0

好的。谢谢。我会检查这一个 –

+0

我试过你的代码。这就是我得到的。 1/1)TokenMismatchException –

+0

然后您需要添加CSRF令牌。 – ub3rst4r