you forgot to define name attribute in your input tag.
May 10, 2017
13
Level 1
Ajax request is empty when it gets to controller
Trying to implement search for quiz app. I have tried to edit the ajax to send the $request to arrive on my controller properly but none of it is working
This is my form
<form class="navbar-form navbar-left" id="searchForm" method="post" action="{{route('quiz.search')}}">
{{ csrf_field() }}
<div class="form-group">
<input type="text" id="query" class="form-control" placeholder="Search">
</div>
<button id="searchSubmit" type="submit" class="btn btn-default">Submit</button>
</form>
and my ajax
$("#searchForm").submit(function(e){
$.ajax({
type:"POST",
url:"quiz/search",
data:$(this).serialize(),
success:function(data){
console.log("we succeded" + data);
}
});
});
public function search(Request $request){
$query = $request->query;
dd($query);
$results = Quiz::where('title','%$query%')->get();
dd($results);
// if (Request::wantsJson()) {
// return response()->json($results);
// }
}
Right now when I dd($query) I am getting,
ParameterBag {#48 ▼
#parameters: []
}
I have tried variations of ajax such as
data:{
'query':$("#query").val(),
'_token':$('input[name=_token]').val()
},
I've also tried adding the e.preventDefault(); at the beginning of the callback function but if I do then my ajax doesn't even reach my controller
Route
Route::post('quiz/search','QuizController@search')->name('quiz.search');
Please or to participate in this conversation.