我有一个简单的搜索字段,它是一个表单。提交时,以下代码负责获取结果。代码给了我一个内部服务器错误。这导致我不在控制器中输入我的PHP搜索功能。我认为它与我的令牌设置有关。它们使用其他ajax调用,但由其他人编码。
注意:此代码用于在我的控制台中打印搜索查询结果。由于更改(通过在主刀片中实现令牌脚本),它停止了这样做。所以我的工作流程停留在发布ajax调用。你能找到什么导致这个错误重新出现或出现吗?提前谢谢。
$("#searchonfrontpage").submit(function(e){
e.preventDefault();
var whattosearch = $("#searchFRONT").val();
console.log("searching... " + whattosearch + ".");
$.ajax({
type: "POST",
url: "/",
data: { whattosearch: whattosearch },
success : function(data){
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
//just in case
Route::filter('csrf', function() {
$token = Request::ajax() ? Request::header('X-CSRF-Token') : Input::get('_token');
if (Session::token() != $token)
throw new Illuminate\Session\TokenMismatchException;
});
//frontpage routes
Route::get('/','frontpageController@frontpage');
Route::post('/', 'frontpageController@search');
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
public function search() {
//not making it here because of the 500 server error with post
print_r("call made");
$q = $_POST["whattosearch"];
print_r($q);
$searchTerms = explode(' ', $q);
$query = DB::table('projects');
foreach($searchTerms as $term){
$query->join('users', 'users.id', '=', 'projects.user_id')
->where('title', 'LIKE', '%'. $term .'%')
->orWhere('tags', 'LIKE', '%'. $term .'%')
->orWhere('body', 'LIKE', '%'. $term .'%')
->orWhere('username', 'LIKE', '%'. $term .'%')
->orderBy('created_at', 'desc');
}
$projects = $query->get();
print_r($projects); //used to be able to see these results, but not anymore
//return data view here (also gives a 500 error when I used to be able to get projects)
//return View::make( 'viewfile' )->with( 'data', $projects );
//return View::make('projects', compact('projects'));
}
答案 0 :(得分:0)
按照此[教程]({{3}})获得结果。不确定最初的问题是什么。