Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

462
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda