本文介绍了405(不允许的方法)Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试使用ajax删除项目时,我在Laravel中得到405(不允许使用的方法).有人帮忙.
I am getting a 405 (Method Not Allowed) in Laravel while trying to delete an item using ajax. Someone please help.
这是我的路线
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 .
The reason you're getting this error is because your request URI /blog/destroy
doesn't match the route definition /destroy
.
因此将路线更改为
Route::post('/blog/destroy', 'PagesController@destroy');
或更改您的请求
$.ajax({
type: 'post',
url: '/destroy',
// ...
})
这篇关于405(不允许的方法)Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!