How to pass laravel CSRF token value to vue

后端 未结 6 682
滥情空心
滥情空心 2020-12-02 22:57

I have this form where the user should only type text inside a text area:

            
{{-- Name of the me
6条回答
  •  不知归路
    2020-12-02 23:27

    Automatic Axios CSRF token attaching Solution:

    first, store the token in an HTML meta tag:

    Then register the CSRF Token as a common header with Axios so that all outgoing HTTP requests automatically have it attached.

    Inside your bootstrapping/main JS file, include:

    let token = document.head.querySelector('meta[name="csrf-token"]');
    
    if (token) {
        window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
    } else {
        console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
    }
    

    Now you can make requests with csrf attached.

提交回复
热议问题