I am trying to send data to a database via ajax. The article page of the article works fine without ajax. I added console.log() just to see something happening, but instead I get this error:
POST http: // localhost / laravel-5 / public / articles / create 500 (Internal server error)
What happened to my code? Is it javascript or a controller?
EDIT: I get this in laravel.log
exception "Illuminate \ Session \ TokenMismatchException" in C: \ xampp \ htdocs \ laravel-5 \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ VerifyCsrfToken.php: 53
Route
Route::resource('articles', 'ArticlesController');
controller
public function store(Requests\ArticleRequest $request) { $article = new Article($request->all()); Auth::user()->articles()->save($article); $response = array( 'status' => 'success', 'msg' => 'Article has been posted.', ); return \Response::json($response); }
JQuery
$(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); } }); });
View
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <h1>Write a New Article</h1> <hr> {!! Form::open(['url' => 'articles', 'id' => 'frm']) !!} <p> {!! Form::label('title', 'Title:') !!} {!! Form::text('title') !!} </p> <p> {!! Form::label('body', 'Body:') !!} {!! Form::textarea('body') !!} </p> <p> {!! Form::label('published_at', 'Date:') !!} {!! Form::input('date', 'published_at', date('Ym-d'), ['class' => 'form-control']) !!} </p> <p> {!! Form::submit('Submit Article', ['id' => 'submit']) !!} </p> {!! Form::close() !!} <h3 id="ajaxResponse"></h3> @if($errors->any()) <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> @endif <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script src="{{ URL::asset('assets/js/ArticleCreate.js') }}"></script>
});