405(不允许的方法)Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44739378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
405 (Method Not Allowed) Laravel
提问by Elizabeth W. Ng'ang'a
I am getting a 405 (Method Not Allowed) in Laravel while trying to delete an item using ajax. Someone please help.
尝试使用 ajax 删除项目时,我在 Laravel 中收到 405(不允许的方法)。有人请帮忙。
Here is my route
这是我的路线
Route::get('/home', 'HomeController@index')->name('home');
Route::post('/destroy', 'PagesController@destroy');
Auth::routes();
Here is my ajax code
这是我的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);
}
})
}
Here is my controller
这是我的控制器
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!' );
}
回答by peterm
The reason you're getting this error is because your request URI /blog/destroy
doesn't match the route definition /destroy
.
您收到此错误的原因是您的请求 URI/blog/destroy
与路由定义不匹配/destroy
。
Therefore either change the route to
因此,要么将路线更改为
Route::post('/blog/destroy', 'PagesController@destroy');
or change your request
或更改您的要求
$.ajax({
type: 'post',
url: '/destroy',
// ...
})
回答by CIPHER SECURITY
Try this for routes->
Route::post('/blog/destroy', 'PagesController@destroy')->(destroyPage);
试试这个路由->
Route::post('/blog/destroy', 'PagesController@destroy')->(destroyPage);
Try this inside ajax:
在 ajax 中试试这个:
$.ajax({
type: 'post',
url:'{{ route('destroyPage') }}',
// ...
})