此路由不支持 GET 方法 Laravel + ConfirmBox

The GET method is not supported for this route Laravel + ConfirmBox

我试图解决这个问题:

The GET method is not supported for this route. Supported methods: POST.

但是我在 Laravel 8 上找不到正确的方法和错误。

这里是blade:

 <a href="{{ route('operDel',$data->id) }}" class="btn btn-danger btn-sm"
     data-tr="tr_{{$data->id}}"
     data-id="{{$data->id}}"
     data-toggle="confirmation"
     data-btn-ok-label="Delete" data-btn-ok-icon="fa fa-remove"
     data-btn-ok-class="btn btn-sm btn-danger"
     data-btn-cancel-label="Cancel"
     data-btn-cancel-icon="fa fa-chevron-circle-left"
     data-btn-cancel-class="btn btn-sm btn-default"
     data-title="Are you sure you want to delete ?"
     data-placement="left" data-singleton="true">Delete</a>

和JavaScript在这个blade

        $(document).on('confirm', function (e) {
            var ele = e.target;
            e.preventDefault();

            $.ajax({
                url: ele.href,
                type: 'DELETE',
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    if (data['success']) {
                        $("#" + data['tr']).slideUp("slow");
                        alert(data['success']);
                    } else if (data['error']) {
                        alert(data['error']);
                    } else {
                        alert('Whoops Something went wrong!!');
                    }
                },
                error: function (data) {
                    alert(data.responseText);
                }
            });
            return false;
        });

这是路线

Route::delete('operDel/{id}', '\App\Http\Controllers\OperationController@destroy')->name('operDel')->middleware('auth');

这是控制器

public function destroy($id)
{

Kvit::where('id', $$id)->delete();
 return response()->json([
                'success'=>"Product Deleted successfully.", 'tr'=>'tr_'.$id
            ]);
            }

我错过了什么?

您需要将方法传递给服务器。

$.ajax({
    url: "operDel/"+id,
    type: 'get',
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
    data: {
        "id": id,
        "_method": 'DELETE',
        "_token": token,
},
success: function(data){
 //do stuff
},
error: function(data){
 //do stuff
}
);

通过此代码解决

Blade

 <button class="btn btn-icon btn-flat-danger deleteRecord"
      data-id="{{$data->id}}">
      <i class="fas fa-trash"></i>
  </button>

JavaScript

$(".deleteRecord").click(function(){
    var id = $(this).data("id");
    var token = $("meta[name='csrf-token']").attr("content");
    if(confirm('Do you want to delete?')){
        $.ajax(
            {
                url: "operDel/"+id,
                type: 'post',
                cache: false,
                data: {
                    "id": id,
                    "method": 'post',
                    "_token": token,
                },
                dataType: "json",
                success: function(response) {
                    if(response.status == "success"){
                        window.location.href='/operations';
                    }else if(response.status == "error"){
                        {toastr['error'](
                            'Please check datas..',
                            'You can not delete this!',
                            {
                                closeButton: true, tapToDismiss: true, progressBar: true,
                            }
                        );
                        }
                    }
                },
            });
    }

});