title: Laravel(ajax) 404 not found
body: <p>I have searched online and the answer for this problem I found is to make sure that I am not using 2 gets or posts with the same name. But I'm not. I am just doing a quick tutorial online on how to use ajax with laravel but I get this error every time I try use post. When I use get it works fine. I would really appreciate it if someone could shed some light on this problem? Thanks.</p>
  
  <blockquote>
    <p>POST <a href="http://ajaxlaravel.app:8000/register" rel="nofollow">http://ajaxlaravel.app:8000/register</a> 404 (Not Found)</p>
  </blockquote>
  
  <p>Here is my welcome.blade file:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Laravel&lt;/title&gt;
      &lt;meta name="csrf-token" content="{{ csrf_token() }}" /&gt;&gt;
  
      &lt;!-- Latest compiled and minified CSS --&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"/&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="container"&gt;
          &lt;h2&gt;Register form&lt;/h2&gt;
  
          &lt;div class="row col-lg-5"&gt;
              &lt;h2&gt;Get Request&lt;/h2&gt;
              &lt;button type="button" class="btn btn-warning" id="getRequest"&gt;getRequest&lt;/button&gt;
          &lt;/div&gt;
  
          &lt;div class="row col-lg-5"&gt;
              &lt;h2&gt;Request Form&lt;/h2&gt;
              &lt;form id="register" action="#"&gt;
                  {{ csrf_field() }}
                  &lt;label for="firstname"&gt;
                      Firstname: &lt;input type="text" id="firstname" class="form-control" placeholder="John"/&gt;
                  &lt;/label&gt;&lt;br&gt;
  
                  &lt;label for="lastname"&gt;
                      Lastname: &lt;input type="text" id="lastname" class="form-control" placeholder="Smith"/&gt;
                  &lt;/label&gt;&lt;br&gt;&lt;br&gt;
  
                  &lt;input type="submit" value="Register" class="btn btn-primary"&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  
  
      &lt;div id="getRequestData"&gt;&lt;/div&gt;
  
      &lt;div id="postRequestData"&gt;&lt;/div&gt;
  
      &lt;script type="text/javascript" src="{{asset('js/jquery.js')}}"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          $.ajaxSetup({
              headers: {
                  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              }
          });
  
          $(document).ready(function(){
              $('#getRequest').click(function(){
                  $.get('getRequest', function(data){ 
                      $('#getRequestData').append(data);
                      console.log(data);
                  });
              });
  
              $('#register').submit(function(){
                 var fname = $('#firstname').val();
                  var lname = $('#lastname').val();
  
                  $.post('register', {firstname:fname, lastname:lname}, function(data){ //Take data from form register and post it but give a call back function using data.
                      console.log(data);
                      $('#postRequestData').html(data);
                  });
              });
          });
      &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my routes:</p>
  
  <pre><code>Route::group(['middleware' =&gt; ['web']], function () {
  Route::get('/', 'WelcomeController@index');
  
  Route::get('/getRequest', function(){
      if(Request::ajax()){ //Does a check to see if the request is ajax.
          return 'getRequest loaded completely';
      }
  
      Route::post('/register', function(){
          if(Request::ajax()){
              return Response::json(Request::all()); 
          }
      });
  });
  });
  </code></pre>
  
  <p>And my WelcomeController:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  
  use App\Http\Requests;
  
  class WelcomeController extends Controller
  {
  public function _construct(){
      $this-&gt;middleware('guest');
  }
  
  /*
   * Show application review
   *
   * @return response
   */
  public function index(){
      return view('welcome');
  }
  }
  </code></pre>
  
  <p>And my VerifyCrsffToken.php:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
  
  class VerifyCsrfToken extends BaseVerifier
  {
  /**
   * The URIs that should be excluded from CSRF verification.
   *
   * @var array
   */
  protected $except = [
      //
  ];
  
  /**
   * Determine if the session and input CSRF tokens match.
   *
   * @param \Illuminate\Http\Request $request
   * @return bool
   */
  protected function tokensMatch($request)
  {
      // If request is an ajax request, then check to see if token matches token provider in
      // the header. This way, we can use CSRF protection in ajax requests   also.
      $token = $request-&gt;ajax() ? $request-&gt;header('X-CSRF-Token') : $request-    &gt;input('_token');
  
      return $request-&gt;session()-&gt;token() == $token;
  }
  }
  </code></pre>