我正在尝试使用 AJAX 搜索我的应用程序,但遇到问题。
如果我做错了什么,请纠正我,我是AJAX新手。
我的路线:
Route::get('/retours/{id}/searchpart/{searchquery}', 'RetoursController@searchpart');
我的表格和搜索表单,变量被注释掉,否则我总是会收到错误并且无法测试。
{!! Form::open(['class' => 'form-horinzontal']) !!}
{!! Form::text('search', null, array('required','class'=>'form-control','placeholder'=>'Zoeken in onderdelen','onkeyup' => 'search_data(this.value, "result")')) !!}
{!! Form::submit('zoek', array('class'=>'btn btn-info form-control')) !!}
{!! Form::close() !!}
<br>
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Artikelcode</th>
<th>Artikelcode verkoop</th>
<th>Omschrijving</th>
<th>Prijs</th>
<th>Actie</th>
</tr>
</thead>
<tbody>
@if(isset($resultquery))
@foreach($resultquery as $result)
<tr>
<td>
<a href="{{ url('/parts', $result->id) }}">
{{ $result->artikelcode }}
</a>
</td>
<td>
{{--{{ $result->artikelcodeverkoop }}--}}
</td>
<td>
{{--{{ substr($result->omschrijving,0,50) }}--}}
</td>
<td>
{{--€ {{ $result->prijs }}--}}
</td>
<td>
{{--<a href="{{ url('/retours/' .$retour->id . '/addpart/'. $result->id) }}" style="margin-right: 10px;" class="pull-right">--}}
<i class="fa fa-plus"></i>
Aan bon toevoegen
</a>
</td>
</tr>
@endforeach
@else
@endif
</tbody>
</table>
<小时/>
当然还有我的 Controller :
public function searchpart(Request $request, $searchquery){
$data = Parts::where('omschrijving','LIKE', '%' .$searchquery.'%')->get();
return view('retour.updatefill')->with('resultquery',$data);
当然是ajax:
function search_data(search_value) {
$.ajax({
url:'searchpart/' + search_value,
method: 'GET'
}).done(function(response){
$('#myTable').html(response);
});
}
有时我会收到 500 Internet 服务器错误
有时我会收到 404 not found
我感觉我做错了。
最佳答案
使用获取变量而不是自定义路由
Route::get('/retours/{id}/searchpart', 'RetoursController@searchpart');
public function searchpart(Request $request){
$searchquery = $request->get('searchquery');
$data = Parts::where('omschrijving','LIKE', '%' .$searchquery.'%')->get();
return view('retour.updatefill')->with('resultquery',$data);
ajax:
function search_data(search_value) {
$.ajax({
url:'{{action("RetoursController@searchpart")}}' ,
data:{searchquery:search_value}
method: 'GET'
}).done(function(response){
$('#myTable').html(response);
});
}
关于jquery - 如何解决 Laravel 中的 AJAX 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653042/