Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

php - POST requests sometimes return code 302 and sometimes code 200

in my laravel project, POST requests sometimes return code 302 and sometimes code 200

When the 302 code is returned, everything is correct. But sometimes the 200 code is returned and this causes the POST data to be printed on the page and cookies/sessions are not set, But then, the SUBSCRIBE method codes are executed and the redirect is done. This problem occurs in all POST requests, only occasionally. And this problem only happens on the real server

How can this problem be solved?

form

<form action="{{ route('subscribe.save') }}" method="post">
    @csrf
    <input name="email" type="text" placeholder="enter email">
    <input type="submit" value="submit">
</form>

controller

    public function subscribe(Request $request)
    {
        $request->validate([
            'email' => 'required'
        ]);

        Subscribe::create([
            'email' => request('email')
        ]);

        return redirect()->back()->with('message', 'ok');
    }

route

Route::post('/subscribe/save', [AppHttpControllersmainIndexController::class, 'subscribe'])->name('subscribe.save');

docker logs -f --details php / (Logs)

 172.22.0.6 -  09/Jan/2021:17:58:27 +0000 "POST /index.php" 302 // Everything is fine
 172.22.0.6 -  09/Jan/2021:17:58:28 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:29 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:32 +0000 "POST /index.php" 302 // Everything is fine
 172.22.0.6 -  09/Jan/2021:17:58:33 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:34 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:39 +0000 "POST /index.php" 200 // Return code 200, problem!!!
 172.22.0.6 -  09/Jan/2021:17:58:39 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:40 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:48 +0000 "POST /index.php" 302 // Everything is fine
 172.22.0.6 -  09/Jan/2021:17:58:48 +0000 "GET /index.php" 200
 172.22.0.6 -  09/Jan/2021:17:58:49 +0000 "GET /index.php" 200
question from:https://stackoverflow.com/questions/65646475/post-requests-sometimes-return-code-302-and-sometimes-code-200

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...