Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Regenerate CSRF Token If ajax request has an error

I'm using ajax request to submit registration form. If a user submit form on first attempt with correct information, the request is working well. But If user made any mistake, correct the information and submit again, the data is inserted but in console.log(data), I'm facing this error:

"CSRF token mismatch."

"\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php". Line: 389.

head.blade.php

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

My Blade view:

$(document).ready(function() {
    $('#send_form').click(function(e) {
    e.preventDefault();
    /*Ajax Request Header setup*/
    $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });

    $('#send_form').html('Sending..');
        /* Submit form data using ajax*/
        $.ajax({
            url: "{{ route('register')}}",
            method: 'POST',
            data: $('#ajax-register-form').serialize(),

            success: function(response) {


            $('#send_form').html('Submit');

            document.getElementById("ajax-register-form").reset();


              },
            error: function(data) {
              var errors = data.responseJSON;
              console.log(errors);
              $('.error-warning').show();

              }
        });
    });
  });

If user has an error on first attempt, the csrf token should regenerate so user can submit the form without refreshing the whole page.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Remove headers from $.ajaxSetup and put it in send_form request like this.

$('#send_form').html('Sending..');
        /* Submit form data using ajax*/
        $.ajax({
            url: "{{ route('register')}}",
            method: 'POST',
            headers: {
             'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data: $('#ajax-register-form').serialize(),

            success: function(response) {


            $('#send_form').html('Submit');

            document.getElementById("ajax-register-form").reset();


              },
            error: function(data) {
              var errors = data.responseJSON;
              console.log(errors);
              $('.error-warning').show();

              }
        });
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!