如何在laravel中修复MethodNotAllowedHttpException

时间:2017-12-16 04:26:25

标签: php laravel

我在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。谁解决了???谢谢你

1 个答案:

答案 0 :(得分: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];
}
}

希望它有所帮助!