【问题标题】:The GET method is not supported for this route Laravel + ConfirmBox此路由 Laravel + ConfirmBox 不支持 GET 方法
【发布时间】:2021-12-18 09:47:17
【问题描述】:

我试图解决这个问题:

此路由不支持 GET 方法。支持的方法: 发布。

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

这是刀片:

 <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

        $(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
            ]);
            }

我错过了什么?

【问题讨论】:

    标签: php laravel methods routes


    【解决方案1】:

    通过这段代码解决

    刀片

     <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,
                                }
                            );
                            }
                        }
                    },
                });
        }
    
    });
    

    【讨论】:

      【解决方案2】:

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

      $.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
      }
      );
      

      【讨论】:

      • 亲爱的@manojkiran-appathurai 你能再检查一下这个JS吗,因为我纠正了一些错误
      猜你喜欢
      • 2021-04-11
      • 2021-08-05
      • 2020-04-03
      • 2020-06-05
      • 2020-03-21
      • 2021-01-10
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多