【问题标题】:405 (Method Not Allowed) Laravel405(不允许的方法)Laravel
【发布时间】:2017-11-28 02:13:56
【问题描述】:

我在尝试使用 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!' );
}

【问题讨论】:

    标签: php ajax laravel


    【解决方案1】:

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

    因此要么改变路线

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

    或更改您的要求

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

    【讨论】:

    • 这意味着它现在可以工作并且您的控制器代码有问题。
    • 这是我的控制器。我似乎找不到任何问题。你能
    • 500 错误是另一个问题。如果上面的答案对您有所帮助(确实如此),则应该对其进行投票并标记为已接受。
    • 已投票。我的控制器有问题吗?请问有人吗?
    • @ElizabethW.Ng'ang'a 显示您收到的确切错误消息。您应该能够在您的日志文件中找到所有详细信息\storage\logs\laravel.log
    【解决方案2】:

    试试这个路由-> Route::post('/blog/destroy', 'PagesController@destroy')->(destroyPage);

    在 ajax 中试试这个:

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

    【讨论】:

      【解决方案3】:

      只需登录我的服务器并禁用 ModSecurity 及其工作即可获得答案,因此,稍后,我将 ModSecurity 配置为在实时服务器上不出现 405 methods not allowed 错误。

      【讨论】:

        猜你喜欢
        • 2020-07-25
        • 2017-05-31
        • 1970-01-01
        • 2018-09-09
        • 2019-05-10
        • 2018-06-15
        • 2023-02-17
        • 2015-10-15
        • 1970-01-01
        相关资源
        最近更新 更多