7

在配置 X-CSRF 字段之后,我正在尝试使用 axios 和最后一个 Laravel 版本 5.5 发出一些请求,并且我的所有代码都很简单:

        axios.post('/post-contact',{name:'Kamal Abounaim'})
        .then((response)=>{
            console.log(response)
        }).catch((error)=>{
            console.log(error.response.data)
        })

但我收到此错误:419(未知状态)应该是什么问题感谢您的回答

4

2 回答 2

14

这是由于 csrf 令牌而发生的。只需在中添加带有 csrf-token 的元标记,<head>然后像这样将该标记添加到 axios 标头中。

// in the <head>
<meta name="csrf-token" content="{{ csrf_token() }}">

<script type="text/javascript">
    // For adding the token to axios header (add this only one time).
    var token = document.head.querySelector('meta[name="csrf-token"]');
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;

    // send contact form data.
    axios.post('/post-contact',{name:'Kamal Abounaim'
    }).then((response)=>{
        console.log(response)
    }).catch((error)=>{
        console.log(error.response.data)
    });
</script>
于 2017-09-18T20:43:47.003 回答
0

419 错误似乎是 Authentification Timeout。您的代码对我来说看起来不错,所以似乎错误与post-contact端点有关?尝试使用 postman 之类的工具单独测试该端点

于 2017-09-06T22:21:05.760 回答