在Laravel 5.5上使用axios发布请求

Kam*_*azz 5 ajax vue.js axios laravel-5.5

在配置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)
        })
Run Code Online (Sandbox Code Playgroud)

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

Abd*_*bab 10

这是因为csrf-token发生的.只需添加带有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>
Run Code Online (Sandbox Code Playgroud)


fre*_*ett 0

419 错误似乎是 Authentification Timeout。您的代码对我来说看起来不错,所以看来错误与端点有关post-contact?尝试在邮递员等工具中单独测试该端点