title: Laravel 5: Ajax Post 500 (Internal Server Error)
body: <p>I'm trying to submit data to the database via ajax. The submit article page works fine without ajax. I've added <code>console.log()</code> just to see if anything is going through, but instead I'm getting this error:</p>
  
  <blockquote>
    <p>POST <a href="http://localhost/laravel-5/public/articles/create" rel="noreferrer">http://localhost/laravel-5/public/articles/create</a> 500 (Internal Server Error)</p>
  </blockquote>
  
  <p>What's wrong with my code? Is it the javascript or the controller?</p>
  
  <p>EDIT: I'm getting this in <code>laravel.log</code></p>
  
  <blockquote>
    <p>exception 'Illuminate\Session\TokenMismatchException' in C:\xampp\htdocs\laravel-5\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:53</p>
  </blockquote>
  
  <p>Route</p>
  
  <pre><code>Route::resource('articles', 'ArticlesController');
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public function store(Requests\ArticleRequest $request)
      {
  
          $article = new Article($request-&gt;all());
          Auth::user()-&gt;articles()-&gt;save($article);
  
          $response = array(
              'status' =&gt; 'success',
              'msg' =&gt; 'Article has been posted.',
          );
          return \Response::json($response);
      }
  </code></pre>
  
  <p>jQuery</p>
  
  <pre><code>$(document).ready(function() {
      $('#frm').on('submit', function (e) {
          e.preventDefault();
          var title = $('#title').val();
          var body = $('#body').val();
          var published_at = $('#published_at').val();
          $.ajax({
              type: "POST",
              url: 'http://localhost/laravel-5/public/articles/create',
              dataType: 'JSON',
              data: {title: title, body: body, published_at: published_at},
              success: function( data ) {
                  $("#ajaxResponse").append(data.msg);
                  console.log(data);
              }
          });
      });
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"&gt;
  
  &lt;h1&gt;Write a New Article&lt;/h1&gt;
  
  &lt;hr&gt;
  
  {!! Form::open(['url' =&gt; 'articles', 'id' =&gt; 'frm']) !!}
      &lt;p&gt;
          {!! Form::label('title', 'Title:') !!}
          {!! Form::text('title') !!}
      &lt;/p&gt;
  
      &lt;p&gt;
          {!! Form::label('body', 'Body:') !!}
          {!! Form::textarea('body') !!}
      &lt;/p&gt;
  
      &lt;p&gt;
          {!! Form::label('published_at', 'Date:') !!}
          {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' =&gt; 'form-control']) !!}
      &lt;/p&gt;
  
      &lt;p&gt;
          {!! Form::submit('Submit Article', ['id' =&gt; 'submit']) !!}
      &lt;/p&gt;
  {!! Form::close() !!}
  
  &lt;h3 id="ajaxResponse"&gt;&lt;/h3&gt;
  
  @if($errors-&gt;any())
      &lt;ul&gt;
      @foreach($errors-&gt;all() as $error)
          &lt;li&gt;{{ $error }}&lt;/li&gt;
      @endforeach
      &lt;/ul&gt;
  @endif
  
  &lt;script src="//code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;/script&gt;
  &lt;script src="{{ URL::asset('assets/js/ArticleCreate.js') }}"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>});</p>