title: Laravel: Page expired on form submit when using ajax
body: <p>I am new to Laravel and stuck in a mentioned problem for last three days. I googled it but did succeed to find any solution.</p>
  
  <p>I have created a form in Laravel and on losing focus of Grower code input, I am fetching customers from the database using an Ajax Get method.<a href="https://i.stack.imgur.com/Kg8mJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kg8mJ.png" alt="Fetched data from database"></a>
  Ajax code is as under:</p>
  
  <pre><code>$.ajaxSetup({
        headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
      });
  $("#txtGrowerCode").focusout(function(){
  var grower_code = $("#txtGrowerCode").val();
  var grower_name = $("#txtGrowerName");
  var father_name = $("#txtFatherName");
  var village_name = $("#txtVillageName");
  var society_name = $("#txtSocietyName");
  var distt_name = $("#txtDisttName");
  $.ajax({
        type : "get",
        url : "api/grower_details/"+grower_code,
        datatype : "json",
        data : $("#hidden").val(),
        success: function(options){
          obj = jQuery.parseJSON(options)
           $.each(obj, function(i,option){
            grower_name.val(option.g_name);
            father_name.val(option.g_father);
            village_name.val(option.v_name);
            society_name.val(option.so_name);
            distt_name.val(option.d_name);
          });
        }
      });
  });
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>when I press submit button to save it into another table, it shows an error page stating <strong>The page is expired due to inactivity</strong></p>
  
  <p><strong>What I have tried till now</strong></p>
  
  <p>It seems this error is because of CSRF protection, so that</p>
  
  <ul>
  <li><p>I have used a hidden input to hold csrf value.</p>
  
  <p><code>&lt;input type="hidden" id="_hidden" name="_hidden" value="{{csrf_token()}}"&gt;</code></p></li>
  <li><p>Added a meta tag</p>
  
  <p><code>&lt;meta name="csrf-token" content="{{ csrf_token() }}"&gt;</code></p></li>
  <li><p>Once I tried to exclude current URL from VerifyCsrfToken.php by this
  I am able to submit the form successfully but it invalidates the
  login-session or destroys my session variables.</p></li>
  <li><p>I removed my Ajax code i.e. did not fetch customers data from the
  database (I filled all fields manually) and then tried to submit the
  form by this data was successfully saved.</p></li>
  </ul>
  
  <p><strong>Codes:</strong></p>
  
  <p><strong>Route File</strong></p>
  
  <pre><code>// Route group soil sample collection
  Route::group(['Middleware'=&gt;['web']],function(){
          Route::GET('/soil_sample',[
          'as' =&gt; 'soil_sample',
          'uses' =&gt; 'soilSampleDetailsController@getSampleDetails',
      ]);
          Route::post('/soil_sample',[
          'uses' =&gt; 'soilSampleDetailsController@createSampleDetails',
      ]);
  });
  </code></pre>
  
  <p><strong>API file</strong></p>
  
  <pre><code>Route::group(['middleware' =&gt; ['web']],function(){
      Route::get('grower_details/{grower}',[
          'as' =&gt; 'grower_details',
          'uses' =&gt; 'soilSampleDetailsController@getGrowerDetails'
      ]);
  });
  </code></pre>
  
  <p><strong>Controller's Code</strong></p>
  
  <pre><code>public function createSampleDetails(){
          $this-&gt;validate(request(),[
              'txtGrowerCode' =&gt; 'required',
              'txtGrowerName' =&gt; 'required',
              'txtFatherName' =&gt; 'required',
              'txtVillageName' =&gt; 'required',
              'txtSocietyName' =&gt; 'required',
  
          ],
              ['txtGrowerCode.required' =&gt; 'Grower Code is required!',
              'txtGrowerName.required' =&gt; 'Grower Name is required!',
              'txtFatherName.required' =&gt; 'Father name is required!',
              'txtVillageName.required' =&gt; 'Village Name is required!',
              'txtSocietyName.required' =&gt; 'Society Name is required']
          );
  
  
          $sampple_detail = new sample_detail;
          $sampple_detail-&gt;addSampleDetails();
  
          return redirect('/soil_sample');
      }
  public function getGrowerDetails(Request $request){     
          if($request-&gt;ajax()){
              $data = new Grower();
              $growerDetails = $data-&gt;getGrowerDetails($request-&gt;grower);
              return json_encode($data-&gt;getGrowerDetails($request-&gt;grower));  
          }   
      }
  </code></pre>
  
  <p><strong>Model file:</strong></p>
  
  <pre><code>public function addSampleDetails()
      {
          $this-&gt;s_grower = request('txtGrowerCode');
          $this-&gt;crop_name = request('txtCrop');
          $this-&gt;save();
      }
  
  
  public function getGrowerDetails($grower_code){
          $grower_details = self::leftJoin('society_master', 'g_soc','so_code')
              -&gt;leftJoin('village_master','g_vill','v_code')
              -&gt;leftjoin('district_master','district_master.d_code','society_master.so_distt_code')
              -&gt;select('grower_master.id', 'grower_master.g_code', 'grower_master.g_name', 'grower_master.g_father','society_master.so_name', 'village_master.v_name', 'district_master.d_name')
                  -&gt;where('grower_master.g_code',$grower_code)
              -&gt;get();
          return $grower_details;
      }
  </code></pre>
  
  <p><strong>Form tag</strong></p>
  
  <pre><code>&lt;form class="form-horizontal" id="frmDocCreate" method = "POST" action="{{route('soil_sample')}}"&gt;
  </code></pre>