首页 > 解决方案 > Laravel React 419 对 POST 请求,而 XCSRF 在标头中

问题描述

如上所述,我每次尝试发布到控制器时都会收到 419。

const handleSubmit = useCallback((_event) => {

    window.axios.defaults.headers.common = {
        'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')
    };

    console.log(document.querySelector('meta[name="csrf-token"]').getAttribute('content'));

    axios.post('/bol/accounts/create', {
        bol_public_key: '12345454',
        bol_private_key: '123454'
    })
    .then(function (response) {
        console.log(response.data);
    })
    .catch(function (error) {
        console.log(error);
    });
}, []);

令牌在请求标头中,所以应该没问题吗?

:authority: *.ngrok.io
:method: POST
:path: /accounts/create
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: nl-NL,nl;q=0.9
cache-control: no-cache
content-length: 86
content-type: application/json;charset=UTF-8
origin: https://8be933f3b3a6.ngrok.io
pragma: no-cache
referer: https://*.ngrok.io/?hmac=564b7bb3c74e59e7a578e50f3bbfb318dfc3d7dea58820d9d66b46ade87b6659&locale=en-NL&new_design_language=true&session=d39033c75fb0a751a23596feae7cb86f704114e33b2bd1c676ddd1bea05ab759&shop=*.myshopify.com&timestamp=1607901296
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
x-csrf-token: tUQk4qMxSNIvTx07oePArv94CEk2LedNmadu2Ozs

标签: reactjslaravelpost

解决方案


推荐阅读