samedi 20 octobre 2018

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. 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.

    $.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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire