405(方法不允许)Laravel

时间:2017-06-24 17:58:29

标签: php ajax laravel

我在尝试使用ajax删除项目时在Laravel中获得405(Method Not Allowed)。有人请帮忙。

这是我的路线

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!' );
}

3 个答案:

答案 0 :(得分:3)

您收到此错误的原因是您的请求URI /blog/destroy与路由定义/destroy不匹配。

因此要么改变路线

Route::post('/blog/destroy', 'PagesController@destroy');

或更改您的请求

$.ajax({
    type: 'post',
    url: '/destroy',
    // ...
})

答案 1 :(得分:0)

尝试此路线-> Route::post('/blog/destroy', 'PagesController@destroy')->(destroyPage);

在ajax中尝试此操作

$.ajax({
    type: 'post',
    url:'{{ route('destroyPage') }}',
    // ...
})

答案 2 :(得分:0)

得到答案后,只需登录我的服务器并禁用ModSecurity及其工作,因此,稍后,我将ModSecurity配置为在实时服务器上不出现405个方法不允许错误。

相关问题