Imi
Imi

Reputation: 549

how to use laravel csrf token inside vuejs component

I am trying to use a form inside vue component. the problem is that it does not accept my csrf token. I have tried to add it multiple ways, using

{{!! csrf_field()!!}} // the component does not render after this

then i tried to add xcsrf

blade.php


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

then adding this to script

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

then i tried adding xcsrf to the mount function like this

mounted() {
        this.csrf = window.laravel.csrfToken        
    } // I get the error csrfToken of undefined

here is my code app.js

//new vue component 
Vue.component('search', require('./components/Searchnames.vue'));

component.vue

<template>
    <div class="row">
        <form class="col-md-12" action='/table' method="POST">

        <input type="hidden" name="_token" :value="csrf">
            <h1 class="mainheading">
                I have found the following names matching your search
            </h1>
            <br/>
            <br/>
            <br/>
            <div class='names_container' v-for="name in names">
                <button class=" btn-default btn-block glow-border names" type='submit' v-on:click="getName">
                {{name.label.eng}}
                </button>

            </div>

        </form>
    </div>

</template>


<script>
  export default {
        data: function () {
            return {
                names: [],
                selected: "",
                csrf: ""
            };

        },
        methods: {
            getData: function () {
                let self = this;
                self.$http.jsonp(url, {
                        jsonpCallback: "JSON_CALLBACK"
                    })
                    .then(response => {
                        response.body.map(function (obj) {
                            if (obj.type == 'org') {
                                self.names.push(obj)
                            }

                        });
                        console.log(JSON.stringify(this.names))
                    })
            },
            getName: function (element) {
                this.selected = element.target.innerText
            }
        },
        mounted: function () {
            this.csrf = window.laravel.csrfToken ;
            this.getData();


        }
    }
</script>

blade.php template

@section('content')
     <div>
         <search></search>
         </div>   
    @endsection

    <script>
        var url = '{{ $url }}'.replace(/&amp;/g, '&');
    </script>

Upvotes: 1

Views: 1708

Answers (1)

Alex Mac
Alex Mac

Reputation: 923

try this

Vue.http.interceptors.push(function (request, next) {
request.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;
next();
});

then try this or add it to your app.blade.php header:

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

Upvotes: 2

Related Questions