2

I've been at it for a while to get this csrf token to work with the vue.js example.

but it keeps saying i don't have a token. I've tried all kinds of variations.

bottom (not head, before end of body)

    <script>
    window.myToken =  <?php echo json_encode([
        '_token' => csrf_token(),
    ]); ?>
    </script>

    <script src="/js/manifest.js"></script>
    <script src="/js/vendor.js"></script>
    <script src="/js/app.js"></script>

html

<div id="app">
@section('content')
<example></example>
@endsection
</div>

console output

CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token

vendor.js:7635 You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html

Example doesn't seem to do much either, but I guess i need the token.

2 Answers 2

10

if you look into

/resources/assets/js/bootstrap.js

You will find

let token = document.head.querySelector('meta[name="csrf-token"]');

You should add in your <head> tag this:

 <meta name="csrf-token" content="{{csrf_token()}}">

This error will not be shown anymore.

I hope this helps

Sign up to request clarification or add additional context in comments.

4 Comments

Mh doesnt seem to work for me either, also tried with csrf_token()
<meta name="csrf-token" content="{{csrf_token()}}"> into <head> works for me maybe <meta name="csrf-token" content="{!!csrf_token()!!}">
@Bas i am happy this helps you
Strange both method's don't seem to work for me, it's also in the docs.
3

Insert this script into your page head tag.

<script>
   window.Laravel = {
      'csrfToken' => '{{csrf_token()}}',
   };
</script>

In your bootstrap.js:

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': window.Laravel.csrfToken,
    'X-Requested-With': 'XMLHttpRequest'
}

2 Comments

Really appreciate if someone tell me ... why this is down voted ?
Doesn't seem to work either, i've seen that window.Laravel method before. I assume the Vue.http should also be in a script tag? I wasn't the one that downvoted though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.