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

460
Visualizações
Message: "CSRF token mismatch.", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}

I am A PHP developer and currently moving towards Laravel framework as per my task I have to complete the realtime table using ajax but I am sticking with an error which is CSRF token mismatch error please help me to resolve the error I am posting shortcode only

JAVA Script

<script>

 function getMessage() {
        $.ajax({
           //var data = {"_token": $('#token').val()},
           type:'POST',
           url:'/getMsg',
           headers: {'XSRF-TOKEN': $('meta[name="_token"]').attr('content')},
           success:function(data) {
              $("#msg").html(data.msg);
           }
           
        });
        
     }
    
  </script>

Route Path

Route::post('/getMsg','CustomerSearchController@doAjaxTest');

Controller code

public function doAjaxTest(){
    $msg = "<b>Message over ajax This test is Successful</b>.";
    return response()->json(array('msg'=> $msg), 200);
}

HTML CODE

<center>
 
 <input type = "hidden" name = "_token" value = '<?php echo csrf_token(); ?>'>
 <table>
 <tr>
 
 <td><label>Enter Place Name</label></td>
 <td><input type="text" class="form-control" id="placename" name="placename" placeholder="Name Of Place"/></td>
 </tr>
 <tr>
 <td>
 <input type="submit" value="Get Message" onclick="getMessage()" />
 </td>
 </tr>
 </table>

 <br>
 <!--
 <div class="panel panel-default table-responsive">
            <div id="dataTag"><b>All the Details according to department will be displayed</b></div>
            
      </div>
 -->
 <div id = 'msg'>This message will be replaced using Ajax. 
     Click the button to replace the message.</div>
  </center>

I don't know why it is showing me CSRF token mismatch when headers contain tokens once solved i can have some realtime action please help out

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

0

I had a same problem. i changed APP_NAME in .env file to default value (Laravel). try it.

over 4 years ago · Santiago Trujillo Relatório

0

make sure that you have the meta tag in your head of the view:

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

Then you can initialize it just once after loading the jQuery library, add this:

<script type="text/javascript">
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
</script>

Try with solution

over 4 years ago · Santiago Trujillo Relatório

0

If you have already configured headers and it still doesn't work, try creating a new key:

php artisan key:generate
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