[英]How to correctly send the CSRF token with Axios, React and Laravel 9. Request failed with status code 419
I'm using React, Inertia, Breeze and Laravel 9. When sending a request to the server with Axios, I get the following error: `{message: 'Request failed with status code 419', name: 'AxiosError', code: 'ERR_BAD_REQUEST'.我正在使用 React、Inertia、Breeze 和 Laravel 9. 使用 Axios 向服务器发送请求时,出现以下错误:`{message: 'Request failed with status code 419', name: 'AxiosError', code: 'ERR_BAD_REQUEST'。
My code is the following (React):我的代码如下(反应):
const handleLogout = (e) => {
e.preventDefault();
const token = document.head.querySelector('meta[name="csrf-token"]').getAttribute('content');
console.log(token);
axios.post('logout', { name: 'John Doe' }, {
headers: {
'content-type': 'text/json',
'X-CSRF-TOKEN': token
}
}
);
};
and in my HTML I have:在我的 HTML 中,我有:
{{-- CSRF Token --}}
<meta name="csrf-token" id="csrf-token" content="{{ csrf_token() }}">
It correctly prints the token in console.log but throws me the error.它在 console.log 中正确地打印了令牌,但抛出了错误。 Something when passing the token does not work for me.传递令牌时有些东西对我不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.