Laravel POST 方法作为 GET 方法发送。代码 405

Posted

技术标签:

【中文标题】Laravel POST 方法作为 GET 方法发送。代码 405【英文标题】:Laravel POST method being sent as GET method. Code 405 【发布时间】:2019-01-23 20:38:54 【问题描述】:

所以我已经查看了与我的问题相关的答案。 我有一个 laravel 应用程序,它在我的本地主机服务器上运行良好。那里的路线运行良好。但是当我在共享主机上上传我的 laravel 应用程序时,只有 GET 方法有效。当我尝试使用任何 POST 请求(例如登录到我的应用程序)时,它会发送 405 错误代码。 所以我会告诉你我登录时出了什么问题。

这是路线。

Route::post('authinticate',['as'=>'authinticate','uses'=>'LoginController@authenticate']);

控制器功能

public function authenticate(LoginRequest $request)
     //$credentials = $request->only('user_name', 'password');

        $credentials = array(
            'user_name' => $request->input('username'),
             'password' => $request->input('password'),
            );


        if (Auth::attempt($credentials)) 
            // Authentication passed...
            return redirect()->route('home');
        
        else
            return redirect()->back()->withErrors(['message' => 'اسم المستخدم او كلمة المرور غير صحيحين.']);
        
    

html 表单:

<form method="POST" action=" route('authinticate') ">
                @csrf

                    <div class="form-group signIn">
                        <label for="username">اسم المستخدم</label>
                        <input type="text" name="username" placeholder="اسم المستخدم" class="form-control" id="username" value=" old('user_name') ">
                    </div>
                    <div class="form-group">
                        <label for="password">كلمة المرور</label>
                        <input type="password" name="password" placeholder="كلمة المرور" class="form-control" id="password">
                        <p></p>
                        <a href=" route('forgot') " >نسيت كلمة المرور</a>
                    </div>
                    <input type="submit" class="btn btn-block btn-default btn-success" name="submit" id="submit" value="دخـــول">
                </form>

这是我在提交表单之前从控制台得到的。

Mixed Content: The page at 'https://www.aouacc.net/login' was loaded over a secure connection, but contains a form that targets an insecure endpoint 'http://www.aouacc.net/authinticate'. This endpoint should be made available over a secure connection.

这是在提交之后。

【问题讨论】:

你能展示你的home 路由和控制器吗?您能否登录您的 authenticate 函数,以确保它是 1)点击该函数和 2)它将重定向到哪个? 【参考方案1】:

检查标头,对http://www.aouacc.net/aunthinticate 的POST 请求返回301 Mover Permanently 标头,这会触发重定向到https 版本。由于显然您没有在代码中设置该重定向,因此它可能已设置在服务器级别。更改您的路线,以便他们使用https://www.aouacc.net 而不是http://www.aouacc.net

【讨论】:

好的,但是我该怎么做呢?,另外我忘了提到我正在使用 cloudflare,也许它传递了一些导致问题的东西? 检查您的 Cloudflare 配置,有一条规则会自动将所有 http 请求重定向到 https。我认为选项是“自动 HTTPS 重写”。如果您确实想使用 https,那么您需要在所有规则中强制使用它。在这里查看答案:***.com/questions/28402726/… 看看如何做到这一点。

以上是关于Laravel POST 方法作为 GET 方法发送。代码 405的主要内容,如果未能解决你的问题,请参考以下文章

当方法更改为 GET 路由不像 POST 方法 Laravel 那样工作

此路由不支持 GET 方法。支持的方法:POST。 laravel 5.8 阿贾克斯

此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。在 laravel 中

在 Laravel auth 中,该路由不支持 POST 方法。支持的方法:GET、HEAD

laravel向我显示此错误此路由不支持POST方法。支持的方法:GET,HEAD,PUT,DELETE

错误:此路由不支持 POST 方法。支持的方法:GET、HEAD。 - 使用 laravel livewire