你好人的我試圖弄清楚Laravel 5.1中的Live feed應用程序,當用戶可以提交他們的帖子。我仍然堅持使用Ajax,因爲我想將數據保存在數據庫使用Ajax請求中。Ajax提交表單Laravel 5.1
這裏是我的形式
{!! Form::open(array('url'=>'saveposts','method'=>'POST')) !!}
<div class="panel-body">
<textarea name="post" class="form-control share-text" rows="3" placeholder="Share your status..."></textarea>
</div>
<div class="panel-footer share-buttons">
<a href="#"><i class="fa fa-map-marker"></i></a>
<a href="#"><i class="fa fa-photo"></i></a>
<a href="#"><i class="fa fa-video-camera"></i></a>
<button type="submit" class="send-btn btn btn-primary btn-xs pull-right display-none" id="submit">Post</button>
{!! Form::close() !!}
這裏是Ajax的腳本
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
url: 'saveposts',
type: "post",
data: {'post':$('input[name=post]').val(), '_token': $('input[name=_token]').val()},
success: function(data){
alert(data);
}
});
});
});
</script>
路線:
Route::post('saveposts', '[email protected]');
而且方法控制器
public function savePosts() {
if(Requestjx::ajax()) {
$data = Input::all();
return Response::json([
'error' => false,
'insertedData' => $data
], 200);
}
}
怎麼了?它沒有工作。任何幫助表示讚賞。謝謝
如果將ajax調用中的url設置爲「/ saveposts」,會發生什麼情況? 「saveposts」不是一個好的路由名稱btw,查找最佳實踐:http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#versioning – sandrooco
你好,謝謝你的回答。沒有改變,如果我添加/ url ... – rubenSousa
它工作?如果不是你得到什麼錯誤? – cmac