Ajax 在 Laravel 中调用 return 错误如 "The POST method is not supported for this route. Supported methods: OPTIONS "

Ajax Call return error like "The POST method is not supported for this route. Supported methods: OPTIONS " in Laravel

我在页面加载时使用 ajax 从服务器获取一些数据。但是这些调用总是 returns 错误为 The POST method is not supported for this route. Supported methods: OPTIONS。我已经尝试了所有可以但没有用的选项。这是我的代码。

        var today = new Date();
        var monthYear = moment(today).format('MMM/YYYY');
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url: baseUrl + 'statisticalboxes',
            method: 'POST',
            data: {
                'project-month': monthYear
            },
            success: function(data) { 
                console.log(data);
            },
            error:function(e) {
                console.log(e);
            }
        });

web.php

Route::post('statisticalboxes', ['as' => 'statisticalboxes','uses' => 'DashboardController@getStasticalBoxData']);

我已经将方法更改为所有其他选项,但没有用。 当我查看网络选项卡时,请求的 url 看起来像 Request URL: http://127.0.0.1:8000/nullstatisticalboxes,但实际是Request URL: http://127.0.0.1:8000/statisticalboxes

您的 baseUrl 变量 return null 值。

你在哪里定义baseUrl?

定义 baseUrl 变量。

--- 或 ---

像这样使用url

url: '/statisticalboxes',