如何在laravel中修复MethodNotAllowedHttpException

问题描述 投票:0回答:1

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

php laravel
1个回答
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];
}
}

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.