我不是一個很有經驗的開發人員,所以我無法弄清楚如何在我的表單中使用ajax。我正在嘗試使用引導程序typeahead。但是我的csrf_token過濾器會導致「500」錯誤。我可以在哪裏發送我的ajax腳本中的csrf_token?如何在typeahead表單中添加csrf_token()?
<script>
$('.typeahead').typeahead({
source : function(typeahead, query){
$.ajax({
url : 'typequery',
type : 'POST',
data : { query : query, column : 'title' },
dataType : 'json',
async : true,
success : function(data) {
return process(data.titles);
}
});
}
});
</script>
這是我需要填寫的表單。它的作品,如果我評論我的後過濾器。
{{ Form::open(array('url' => 'searchblogs' ,'id' => "search", 'class' => "input-append")) }}
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input id="typeahead" size="400" type="text"
data-provide="typeahead" data-items="4" />
<input class="btn search-bt" type="submit" name="submit" value="" />
{{ Form::close() }}
這裏是我的控制器
public function postTypeahead()
{
$query = Input::get('query');
// Search the database.
//
$results = Post::select('title')->where('title', 'LIKE', '%' . $query . '%')->take(5)->get();
$data = array();
// Loop through the results.
//
foreach ($results as $result):
$data[] = $result->title;
endforeach;
// Return a response.
//
return Response::json($data);
感謝幫助。
你的服務器端在做什麼? –
我粘貼了我的控制器。 – ytsejam
明白你的意思是什麼以色列? – ytsejam