I have a Vue.js component...
<template>
<form method="POST" action="/login">
<button class="btn btn-primary center-block" @click="$emit('buttonClicked')">
Login
</button>
</form>
</template>
<script>
var axios = require('axios');
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');
export default {
created: function () {
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
},
props: ['show'],
name: "login-component",
methods: {
close: function () {
this.$emit('close');
},
buttonClicked: function () {
axios.post('/login', {data: this.data})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error.message);
});
}
}
}
</script>
My csrf exists in meta:
<meta id="token" name="csrf-token" content="{{ csrf_token() }}">
But it doesn't pass with the Ajax request instead I get...
419 unknown statusand no_tokenin console->Form Data
What am I doing wrong?
I tried to use...
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
};
But it didn't help.
require('axios')axios is only available in the same scope, it isn't hoisted towindow. Try your latter attempt but withoutwindow.. | Also remove the;, it is invalid syntax (don't forget to check the console please, it have surely shown errors with both issues).axios.defaults.headers.common = { 'X-Requested-With': 'XMLHttpRequest', 'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content'), };but didn't helped