【问题标题】:how to fix MethodNotAllowedHttpException in laravel如何在 laravel 中修复 MethodNotAllowedHttpException
【发布时间】:2017-12-16 04:26:25
【问题描述】:

我正在 laravel 中创建函数删除,这是我的控制器

public function removePetitions($id)
{
    $rm = Petitions::find($id);
    $rm->delete();
    return ['status' => true];
}

这是我的路线

Route::post('/delete/petition/{id}','Admin\PetitionController@removePetitions')->name('admin.delete');

当我在视图中单击按钮删除时,它会显示 MethodNotAllowedHttpException。有人解决吗???谢谢你

【问题讨论】:

  • 在您使用 id 的方式中,我认为您已接到电话,因此请尝试使用 Route::get...
  • 我使用 ajax 提交操作,而 ajax 中的方法我使用 POST
  • 如果您使用的是ajax,您是否在通话中发送了CSRF令牌?
  • 鉴于我使用了 Del 它是我在路由中定义的响应权 URL,但它仍然无法正常工作
  • 您正在使用 ajax 调用删除记录? @NguyễnMinhHuy

标签: php laravel


【解决方案1】:

如果我了解您的问题,您正在寻找这种东西

Your anchor tag here look like this:-
<a href="javascript:;" class="pull-right btn-del-petition" data-id="{{$petition->id}}">Del</a>
And route looklike this :-
Route::post('/delete/petition','Admin\PetitionController@removePetitions');

现在是 ajax 代码:-

<meta name="csrf-token" content="{{ csrf_token() }}" /> // add in head tag
$.ajaxSetup({
headers:{
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}); 
$(document).on('click','.btn-del-petition',function(){
var id = $(this).attr('data-id');
$.ajax({
    type: 'post',
    url: 'delete/petition',
    data: {id :id},
    success:function(resp){
        alert(resp);
        //Delete that deleted row with jquery
    },
    error:function(){
        alert('Error');
    }
})
})

现在你的功能:-

public function removePetitions(Request $request)
{
if($request->ajax()){
    $data = $request->all();
    $rm = Petitions::find($data['id']);
    $rm->delete();
    return ['status' => true];
}
}

希望对你有帮助!

【讨论】:

  • 要解决这个问题,是否需要将路线从 '/delete/petition/{id}' 更改为 '/delete/petition'
  • 立即查看我的路线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2013-07-04
  • 2015-09-11
  • 2017-12-21
  • 2018-02-28
  • 2016-08-31
相关资源
最近更新 更多