Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

423
Visualizações
CSRF token mismatch error in laravel 5 on multiple async ajax request?

I'm using Laravel 5.2 for my web application and I have a page with multiple ajax requests by the same event. In $.ajax, I set async: true, Sometimes it shows CSRF token Mismatch error and redirect to login page. However when I set Async: false in ajax, it works fine but it takes lots of time.

Please help me so that it does not show token mismatch error.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

in your form create one hidden filed name _token you can use this helper method to generate field

{!! csrf_field() !!}

in javascript you have to fetch this field value

var token = $( "input[name='_token']" ).val();

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston",_token:token }
});

another way create hidden a span or div add data attribute to it

<div id="token" data-token="{{ csrf_token() }}"></div>

fetch in javascript data value

var token = $( "#token" ).data('token');

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston",_token:token }
});
over 4 years ago · Santiago Trujillo Relatório

0

You said you use

$.ajaxSetup({ headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') } });

Maybe somewhere headers in request overrides, try to change it on

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('X-CSRF-Token', $('meta[name=_token]').attr('content') );
    }
});
over 4 years ago · Santiago Trujillo Relatório

0

Please modify your url variable like so :

url: '/my-route'+'?_token=' + '{{ csrf_token() }}',
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda