简体   繁体   English

AJAX 请求中不允许使用 Laravel 405 方法

[英]Laravel 405 method not allowed on AJAX request

The code works just fine on my local dev environment, but gives a 405 method not allowed exception when I upload it to the server.该代码在我的本地开发环境中运行良好,但在我将其上传到服务器时给出了405 method not allowed异常。 The solutions on this question and this question didn't work for me. 这个问题和这个问题的解决方案对我不起作用。

Here's the controller.这是控制器。

    public function DeleteMultipleProjects(Request $request){
        $json = $request->projectsToDelete;
        $to_delete = collect([]);

        $projects = json_decode($json);
        foreach ($projects as $project) {
            $to_delete->push($project->id);
        }

        Project::destroy($to_delete);
    }

Here's the corresponding entry in the routes file.这是路由文件中的相应条目。

Route::post('/ajax/dashboard/delete-projects', 'ProjectController@DeleteMultipleProjects');

And here's the AJAX call to the URL.这是对 URL 的 AJAX 调用。

    $.ajax({
        url: '/ajax/dashboard/delete-projects',
        method: 'POST',
        data: {
            projectsToDelete: JSON.stringify(vThis.selectedProjects),
        },
        complete: function(){
            vThis.refreshProjects();
        }
    });

This is what the data looks like for an example test case.这是示例测试用例的数据。

[{"id":140,"name":"a","updated_at":"Sun, Oct 21, 2018 4:31 AM","selected":true,"selectHovering":false},{"id":139,"name":"New Project","updated_at":"Sun, Oct 21, 2018 4:31 AM","selected":true,"selectHovering":false}]

How can I get past this error, and why does this only happen in production?我怎样才能克服这个错误,为什么这只发生在生产中?

Edit It's worth noting that I have other AJAX calls to routes throughout my application, and they work fine.编辑值得注意的是,我对整个应用程序中的路由进行了其他 AJAX 调用,并且它们工作正常。

It seems you are missing the csrf token您似乎缺少 csrf 令牌

Update your ajax post data as:将您的 ajax 帖子数据更新为:

$.ajax({
    url: '/ajax/dashboard/delete-projects',
    method: 'POST',
    data: {
        projectsToDelete: JSON.stringify(vThis.selectedProjects),
        _token: '{{ csrf_token() }}',
    },
    complete: function(){
        vThis.refreshProjects();
    }
});

A 405 error in Laravel means the route does not exist for the HTTP method you are using. Laravel 中的 405 错误意味着您使用的 HTTP 方法不存在该路由。

If you are receiving this error on production but not locally, it would indicate your route files are either outdated or have been cached.如果您在生产中但不是在本地收到此错误,则表明您的路由文件已过时或已被缓存。

Clear your route cache with:使用以下命令清除路由缓存:

php artisan route:clear

Verify your POST route exists with:使用以下命令验证您的 POST 路由是否存在:

php artisan route:list --path=ajax/dashboard/delete-projects

If route:list confirms your POST route exists but you're still receiving a 405, the other thing that can lead to cached routes is opcache without revalidating timestamps, in which you may need to restart your php processes to clear.如果 route:list 确认您的 POST 路由存在但您仍然收到 405,则可能导致缓存路由的另一件事是 opcache 而不重新验证时间戳,您可能需要重新启动 php 进程才能清除。

The problem was in the data that I was sending.问题出在我发送的数据中。 I'm not sure why, but my server didn't like the entire projects array, so I updated it to send just an array of the id s I'd like to delete, and consequently updated the controller to just destroy the request.我不知道为什么,但我的服务器不喜欢整个项目数组,所以我更新它只发送一个我想删除的id数组,并因此更新控制器以destroy请求。 If someone has an explanation, I'm curious.如果有人有解释,我很好奇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM