我正在尝试通过ajax将数据提交到数据库.提交文章页面没有ajax就可以正常工作.我添加了console.log()只是为了看看是否有任何事情发生,但我得到了这个错误:
POST 07000 500 (Internal Server Error)
我的代码出了什么问题?是javascript还是控制器?
编辑:我在laravel.log中得到这个
exception ‘Illuminate\Session\TokenMismatchException’ in C:\xampp\htdocs\laravel-5\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:53
路线
Route::resource('articles', 'ArticlesController');
调节器
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);
}
});
});
视图
Write a New Article
{!! Form::open(['url' => 'articles', 'id' => 'frm']) !!}
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title') !!}
{!! Form::label('body', 'Body:') !!}
{!! Form::textarea('body') !!}
{!! Form::label('published_at', 'Date:') !!}
{!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
{!! Form::submit('Submit Article', ['id' => 'submit']) !!}
{!! Form::close() !!}
@if($errors->any())
@foreach($errors->all() as $error)
{{ $error }}@endforeach
@endif
});