@extends('layouts.app')
@section('content')
{{--<p>{{$news}}</p>--}}
<p class="container">
<p class="col-lg-2"></p>
<p class="col-lg-8" id="test">
<p class="form">
<input type="text" name="title" id="title" class="form-control">
<textarea name="tcontent" id="tcontent" cols="30" rows="10" class="form-control"></textarea>
<button id="submit" class="btn-primary">提交</button>
</p>
<p id="test1"></p>
</p>
</p>
<script type="text/javascript">
$(document).ready(function (){
$('submit').click(function (){
var data = {
title: $('#title').val(),
tcontent: $('#tcontent').val()
};
$.ajax({
type: "post",
url: 'news/news',
data: data,
dataType: 'json',
headers: {
'X-CSRF-TOKEN': '{{csrf_token()}}'
},
success: function(data){
console.log(data);
var test1 = '<h1> add success</h1>';
console.log(test1);
$('#test').append(test1);
},
error: function (data){
console.log(data);
var test1 = '<h1>add faild</h1>';
console.log(test1);
$('#test').append(test1);
}
});
});
});
</script>
@endsection
public function store(Request $request)
{
$user = Auth::user();
$this->validate($request,[
'title'=>'required',
'tcontent'=>'required'
]);
$news = new News();
$news->title = $request->get('title');
$news->content = $request->get('tcontent');
$news->author = $user->name;
$news->save();
return $news;
}
Auth::routes();
Route::get('/','HomeController@index')->name('home.page');
Route::group(['prefix'=>'back'], function (){
Route::resource('/news/news','News\NewsController');
Route::group(['prefix'=>'commodity', 'namespace'=>'commodity'], function (){
Route::resource('/classify','ClassifyController');
Route::resource('/commodity','CommodityController');
});
});
程式碼如上,點擊提交按鈕沒有任何反應,資料庫中也沒有插入記錄,jQuery也已經在母頁header裡引入了,頁面代碼、控制器、路由設定如上,我實在不知道哪裡出問題了,請大神們解答