我正在尝试通过获取它来从 laravel api 获取数据。在邮递员中,即使在获得圣所授权的情况下,它也可以正常工作。
当我使用 post 方法获取时,它给出:
Failed to load resource: the server responded with a status of 419 (unknown status)
接着:
message: "CSRF token mismatch." ...
这是我的 api 路由文件:
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{id}/edit', [PostController::class, 'edit']);
Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
并获取:
export const addPost = async (post) => {
let resp = await fetch(`http://127.0.0.1:8000/api/posts`, {
method: "POST",
body: JSON.stringify(post),
headers: { Accept: "application/json", "Content-Type": "application/json" },
});
let jsData = await resp.json();
return jsData;
};
感谢帮助