Laravel 发布请求发送 302

时间:2021-05-03 13:23:37

标签: php reactjs laravel inertiajs

在 Laravel 8 中,我尝试使用 React 和 InertiaJS 进行自定义身份验证。我制作了登录页面,其中包含一个表单和方法 export default function Login (props) { const [fields, handleField] = useFormFields({ mail: "diamond.kunde@example.com", password: "password" }); const handleSubmit = (event) => { event.preventDefault(); axios.post('/newurl', fields).catch(console.log); axios.post('/whynotworking').catch(console.log); fetch('/texttext', { method: "POST", body: JSON.stringify(fields) }).then(resp => { console.log(resp); }); }; return ( <Master> ... </Master> ); }

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;

Route::get('/login', [AuthController::class, 'index']);

Route::post('/texttext', [AuthController::class, 'texttext']);
Route::post('/newurl', [AuthController::class, 'newurl']);
Route::post('/whynotworking', [AuthController::class, 'whynotworking']);

我的 web.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class AuthController extends Controller
{
//    public function __construct()
//    {
////        $this->middleware('guest')->except('logout');
//    }

    public function index()
    {
        return Inertia::render('Auth/Login');
    }

    public function texttext()
    {
        dd('texttext');
    }

    public function whynotworking()
    {
        dd('whynotworking');
    }

    public function newurl()
    {
        dd('newurl');
    }
}

还有我的控制器

php artisan route:clear

为什么对索引的 GET 请求有效?但是 POST 没有?

我使用了以下命令:

php artisan config:cache php artisan view:clear php artisan route:cache php artisan optimize php artisan config:clear /login

每个请求都会返回 302 响应,然后向 login 返回一个新请求。 <table th:if="${opleidingen.size() != 0}"> <theader> <tr> <th>Code</th> <th>Titel</th> <th>Thema</th> <th>Delete</th> <th>Pas Aan</th> </tr> </theader> <tbody> <tr th:each="opleiding: ${opleidingen}"> <td><span th:text="${opleiding.getCode()}"/></th> <td><span th:text="${opleiding.getTitel()}"/></th> <td><span th:text="${opleiding.getThema()}"/></th> <td><a href="/admin.html"><img src="../static/Delete.gif"/></a></th> <td><a href="/edit.html"><img src="../static/Edit.gif"/></a></th> </tr> </tbody> </table> 是我提出请求的路径。

1 个答案:

答案 0 :(得分:0)

我终于解决了。

添加标题

'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content')?.toString(),

您的请求和表单不需要 _token

和文件 config/session.php

'secure' => env('SESSION_SECURE_COOKIE', false),

将其设置为 false

相关问题