尝试使用ajax删除项目时,我在Laravel中得到405(不允许使用的方法)。有人请帮忙。
这是我的路线
Route::get('/home', 'HomeController@index')->name('home');
Route::post('/destroy', 'PagesController@destroy');
Auth::routes();
这是我的ajax代码
function confirmDelete(id){
//alert('Delete post id-'+id);
$.ajax({
type: 'post',
url: 'blogs/destroy',
data: {'id' : id},
dataType: 'json',
cache: false,
success: function(res){
console.log("worked");
alert(res);
}
})
}
这是我的控制器
public function destroy (Request $request){
$id = $request->id;
echo json_encode ($id);
// $blog = Blog::findorFail ( $id );
// $blog->delete ();
// return response(['msg'=>'Post deleted',
'status'=>'success']);
// return redirect::to ( '/blogs' )->with ( 'success', 'Post
successfully deleted!' );
}
出现此错误的原因是因为您的请求URI/blog/destroy
与路由定义不匹配/destroy
。
因此,要么将路线更改为
Route::post('/blog/destroy', 'PagesController@destroy');
或更改您的要求
$.ajax({
type: 'post',
url: '/destroy',
// ...
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句