jquery - 如何解决 Laravel 中的 AJAX 搜索?

标签 jquery ajax laravel

我正在尝试使用 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);

enter image description here

当然是ajax:

function search_data(search_value) {
        $.ajax({
            url:'searchpart/' + search_value,
            method: 'GET'
        }).done(function(response){
            $('#myTable').html(response);          
        });
    }
  • 有时我会收到 500 Internet 服务器错误

  • 有时我会收到 404 not found

我感觉我做错了。

madalin 请求的错误: enter image description here

最佳答案

使用获取变量而不是自定义路由

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/

相关文章:

jquery - 在 jquery datepicker 中仅提供当月的半个月日期

javascript - 组合 jquery 函数 - on() hover/mouseenter/mouseleave

javascript - 使用 AJAX 和 PHP 登录时获取用户配置文件

sql - Laravel Eloquent 选择 CASE?

mysql - laravel 条件数据库查询

javascript - 正在导入 Jquery 但 AJAX 不工作?

javascript - jQuery 从 $(this) 获取最接近的同级元素不起作用

javascript - 为什么removeClass()和addClass()不起作用?

javascript - jquery .load() 不加载外部 JS

php - laravel 5.4 中两种不同的身份验证模型