Problème CSRF token mismatch

Par Clayton, il y a 6 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

Donc le X-CSRF-TOKEN fonctionne bien je l'init bien pour axios

Mon code dans le bootstrap.js

window.axios = require('axios'); window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; window.axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; window.axios.defaults.headers.common.crossDomain = true; window.axios.defaults.baseURL = '/api'; /** * Next we will register the CSRF Token as a common header with Axios so that * all outgoing HTTP requests automatically have it attached. This is just * a simple convenience so we don't have to attach every token manually. */ let token = document.head.querySelector('meta[name="csrf-token"]'); if (token) { window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': token.content } }); } else { console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token'); }

Mon code pour poster:

ADD_PRODUCT({commit}, product) { return new Promise((resolve, reject) => { axios.post('/product/create', product) .then(response => { resolve(response) }).catch(err => { reject(err) }) }) },

Ce que je veux

Que je puissent poster mes données sans avoir cette foutue erreurs

Ce que j'obtiens

CSRF token mismatch.", exception: "Symfony\Component\HttpKernel\Exception\HttpException

2 réponses

Guilome, il y a 6 ans

T'as bien mis la balise meta correspondant au token dans le head?

Clayton, il y a 6 ans

J'ai régler le soucis j'avais complétement oublié que dans mes routes de mon api il y avais middleware('web') et qui grouper toutes routes :/