【问题标题】:The POST method is not supported for this route even though i am using get method即使我使用的是 get 方法,此路由也不支持 POST 方法
【发布时间】:2021-02-16 07:05:06
【问题描述】:

我的路线是

Route::get('functionvalueupdate',[\App\Http\Controllers\AdminController::class,'functionvalueupdate'])->name('functionvalueupdate');

在脚本中

$.ajax({
        type: "GET",
        url: "{{route('functionvalueupdate')}}",
        data: {
            id: id,
        },
        success: function(res) {

        }
    }
)

控制器

 public function functionvalueupdate(Request $request)
{
    dd($request);
}

控制台日志错误

allfunctionalarea:1 POST http://localhost/FESROZGAR/allfunctionalarea 405 (Method Not Allowed)

我仍然收到此错误 此路线不支持 POST 方法。支持的方法:GET、HEAD。

【问题讨论】:

  • 检查是否有另一个使用相同uri定义的路由,然后尝试php artisan route:clear
  • 仍然没有运气 :( 我真的厌倦了我在做什么样的错误或愚蠢的错误......
  • 首先检查路由php artisan route:list你的路由是否存在。如果没有,则运行php artisan route:cache
  • 它确实存在 :(

标签: php ajax laravel


【解决方案1】:

这里有点不对。

您的路线,显示 GET 到 functionvalueupdate

但是,您的控制台日志显示在不同的路线上。这是

http://localhost/FESROZGAR/allfunctionalarea 。我没有看到任何通往functionvalueupdate 的路线。

或者类似http://localhost/FESROZGAR/functionvalueupdate

请仔细检查您的 POST 或 GET 到正确的路线。您可以检查元素以查看 {{ route('functionvalueupdate') }} 是否打印正确的路线。

【讨论】:

    【解决方案2】:

    更改 get 以发布如下路线:

     Route::post('functionvalueupdate'[\App\Http\Controllers\AdminController::class,
    'functionvalupdate'])->name('functionvalueupdate');
    

    还将 ajax 类型从 get 更改为 post,并且不要忘记在 ajax 请求中包含 csrf 令牌。

    $.ajax({
            type: "POST",
            url: "{{route('functionvalueupdate')}}",
            data: {
                id: id,
                "_token": "{{ csrf_token() }}",
            },
            success: function(res) {
                console.log(error);
            },
            error:function(error) {
                console.log(error);
            }
        }
    )
    

    【讨论】:

    • 还是没有运气,这个错误在我不知道我错过了什么之前从来没有发生过
    • 请参考此链接您面临同样的问题。 stackoverflow.com/questions/25312766/…
    • 但我使用了正确的方法 :( 我在两种方法中都有 get 方法
    • 使用路由名称传递您的 id 并从控制器函数中删除请求 dd($id);
    【解决方案3】:

    试试这个让调试更好

    $.ajax({
            type: "GET",
            url: "{{route('functionvalueupdate')}}",
            data: {
                id: id,
            },
            success: function(res) {
                console.log(res);
            },
            error:function(error) {
                console.log(error);
            }
        }
    )
    

    请在您的问题中添加 console.log 错误并将您的控制器也添加到您的问题中

    【讨论】:

    • 试过还是没有运气,参数 (res) 我知道它现在没有用,但问题在于 post 方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2020-09-04
    • 2019-08-28
    • 2021-04-11
    • 2019-08-31
    • 2021-05-05
    相关资源
    最近更新 更多