'Axios POST Error: Request failed with status code 419

I am trying to make a post request with Axios + Laravel 7. And I got this error message:

Error: Request failed with status code 419

My Javascrip code:

axios.defaults.headers.common = {
    'X-Requested-With': 'XMLHttpRequest',
    'X-CSRF-TOKEN': window.csrf_token
};
axios.defaults.withCredentials = true;

var formEditEmpresa = document.getElementById("formeditEmpresa");

function validareditEmpresa() {

    var formData = new FormData(formEditEmpresa);

    axios({
        method: 'post',
        url: "editarLoja",
        headers: { "Content-Type": "multipart/form-data" },
        data: formData,
    })
        .then(res => {
            // listarChamados.innerHTML = res.data;
            console.log(res);
        })
        .catch(err => {
            console.error(err);
        })
    return false;
}


Solution 1:[1]

Try changing the headers to this syntax:

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

As show in Laravel docs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yogev D.