2

I'm trying to make a POST using VueJS 1.0.28 but I'm getting a Laravel 5.3 TokenMismatchException error.

This is in app.js to cover both jQuery and Vue:

Vue.http.interceptors.push((request, next) => {
    request.headers.set('X-CSRF-TOKEN', Laravel.csrfToken);

    next();
});

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

This is in the main.blade.php layout where we instantiate the Laravel value:

<script src="{!! asset('/js/app.js') !!}"></script>
<script>
    (function () {
        window.Laravel = {
            csrfToken: '{{ csrf_token() }}'
        };
    })();
</script>

When I look at the headers in the POST request, I don't see the X-CSRF included for Vue.

This is super basic stuff for jQuery and I'm a stumped what I misconfigured on the Vue side. Ideas?

2 Answers 2

1
  1. Move to Vue 2.* if possible.
  2. Don't mix Vue and jQuery. I promise, it will make your life easier.
  3. For handling AJAX use e.g. AXIOS
  4. If so, you will have just to use: axiosDefaults.headers.common['X-CSRF-Token'] = csrfToken;
Sign up to request clarification or add additional context in comments.

1 Comment

#1 not an option right now. #2 requirement of the project due to dependencies. #3 I'll look into it, but have no control over which lib the various packages use.
0

As it turns out, both Vue and jQuery were getting the token, but the package code I was using was directly using XMLHttpRequest so it needed to get the token manually by doing:

xhr.setRequestHeader('X-CSRF-TOKEN', Laravel.csrfToken);

Comments

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.