title: Laravel AJAX 500 Internal Server Error, Tokens Match
body: <p>When I submit the form, I get this error and the page automatically reloads, but the url in the browser then shows my route and content that I posted in the form. Then, if I go ahead and submit again without reloading the page it works just fine. Could it be that I'm not posting the token itself? I have added the meta tag to the head.</p>
  
  <pre><code>&lt;meta name="csrf-token" content="{{ csrf_token() }}" /&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>$.ajaxSetup({
      headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
  });
  
  $('#postForm').submit(function(){
  
      var body = $('#postbody').val();
      var profileId = $('#user_id').text();
  
          $.ajax({
              type: "POST",
              url: "/post/"+profileId,
              data: {post:body, profile_id:profileId},
              success: function(data) {
                  console.log(data);
              }
          });
  });
  </code></pre>
  
  <p>Route:</p>
  
  <pre><code>Route::post('/post/{id}', [
      'uses' =&gt; '\App\Http\Controllers\PostController@postMessage',
      'as' =&gt; 'post.message',
      'middleware' =&gt; ['auth'],
  ]);
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public function postMessage(Request $request, $id)
  {
      if(Request::ajax())
      {
          $this-&gt;validate($request, [
              'post' =&gt; 'required|max:1000',
          ]);
  
              $newMessage = Auth::user()-&gt;posts()-&gt;create([
                  'body' =&gt; $request-&gt;input('post'),
                  'profile_id' =&gt; $id
              ]);
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;form role="form" action="#" id="postForm"&gt;
      &lt;div class="feed-post form-group"&gt;
          &lt;textarea class="form-control feed-post-input" id="postbody" name="post"&gt;&lt;/textarea&gt;
          &lt;div class="btn-bar"&gt;
              &lt;button type="submit" class="btn btn-default btn-post"&gt;&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;input type="hidden" name="_token" value="{{ csrf_token() }}"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>So, the log says that "Request::ajax() should not be called statically" in my controller. I removed that code and it works fine now. However, I want to know if removing it is ok to do of if there's a better way to resolve this. Thanks!</p>
  
  <p>ANSWER: It works by changing</p>
  
  <pre><code>if (Request::ajax()){
      // code...
  }
  </code></pre>
  
  <p>to</p>
  
  <pre><code>if ($request-&gt;ajax()){
      // code...
  }
  </code></pre>