Laravel搜索功能中的Ajax无法正常工作

时间:2019-09-29 07:58:43

标签: ajax laravel

现在它没有显示任何数据;

控制台: finished loading: GET "http://todolist.local/teachers/search?text=a".

当用户在搜索中输入内容时,我试图在tbody中显示结果。

Ajax代码:

<script>
$(document).ready(function(){
        $('#searchname').on('keyup', function(){
                var text = $('#searchname').val();
                $.ajax({
                        type:"GET",
                        url: 'teachers/search',
                        data: {text: $('#searchname').val()},
                        success:function(data){
$('tbody').html(data);
}
                });
        });
});
</script>

web.php:

Route::get('/search', 'TeachersController@ajaxsearch');

搜索控制器:

public function ajaxsearch(){
$searchname = Input::get ( 'searchname' );
  if($searchname != ""){
  $teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname . '%' )->paginate(10);
  return response()->json($teacher);
}
}

视图:

<div class="input-group stylish-input-group">
                        <input type="text" id="searchname" name="searchname" class="form-control"  placeholder="Search..." >
                   <span class="input-group-addon">
                            <button type="submit" class="btn btn-primary">
                                <span class="glyphicon glyphicon-search">Search</span>
                            </button>
                        </span>
                    </div>

5 个答案:

答案 0 :(得分:3)

这应该可以解决问题

HTML:

  <div id="datasearch"></div>

JS:

$(function(){
    $('#searchname').on('keyup', function(){
        $.get('/teachers/search/'+$(this).val(), function(response){
            $('#datasearch').html(response);
        });
    });
});

控制器:

public function ajaxsearch(string $value = null){
    return $value ? Teacher::whereRaw("UPPER(efirst) LIKE '%".strtoupper($value)."%'")->paginate(10) : [];
}

路线:

Route::get('/teachers/search/{value?}', 'TeachersController@ajaxsearch');
  

聊天后版本

HTML:

  <ul id="datasearch"></ul>

JS:

$(function(){
    var $datasearch=$('#datasearch');
    $('#searchname').on('keyup', function(){
        $.get('/teachers/search/'+$(this).val(), function(teachers){
            $datasearch.empty();
            for (var i=0; i<teachers.length; i++){
                $datasearch.append('<li>'+teachers[i].efirst+' <a href="/teachers/edit/'+teachers[i].id+'">edit</a></li>');
            }
        });
    });
});

控制器:

public function ajaxsearch(string $value = null){
    return $value ? Teacher::select('id','efirst')->whereRaw("UPPER(efirst) LIKE '".strtoupper($value)."%'")->offset(0)->limit(10)->get() : [];
}

路线:

Route::get('/teachers/search/{value?}', 'TeachersController@ajaxsearch');

答案 1 :(得分:0)

您知道jquery ajax中的错误功能吗?

$(document).ready(function(){
        $('#searchname').on('keyup', function(){
                var text = $('#searchname').val();
                $.ajax({
                        type:"GET",
                        url: 'teachers/search',
                        data: {text: $('#searchname').val()},
                        success:function(data){$('tbody').html(data);},
                        error:function(jqXHR){alert(jqXHR.status);} 
                });
        });
});

您可以尝试执行此操作,如果有一些键,并且文本应该以逗号分隔,则它将显示错误,因为它是键值对。

答案 2 :(得分:0)

在搜索控制器中,您正在访问错误的输入名称。应该是这样

public function ajaxsearch(){
$searchname = Input::get ( 'text' );
  if($searchname != ""){
  $teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname . '%' )->paginate(10);
  return response()->json($teacher);
}
}

答案 3 :(得分:0)

尝试通过此密码令牌和正确的路线

  <script>
    $(document).ready(function(){
            $('#searchname').on('keyup', function(){
                    var text = $('#searchname').val();
                    $.ajax({
                            type:"GET",
                            headers: {
                              'X-CSRF-TOKEN': '{{ csrf_token() }}'
                            },
                            url: "{{ route('teachers/search') }}",
                            data: {text: $('#searchname').val()},
                            success:function(data){
    $('tbody').html(data);
    }
                    });
            });
    });
    </script>

以及您的控制器部分

public function ajaxsearch(){
    $searchname = Input::get ( 'searchname' );
      if($searchname != ""){
      $teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname . '%' )->paginate(10);
     $teacher->appends(['searchname' => $searchname ]);
      return response()->json($teacher);
    }
    }

答案 4 :(得分:0)

首先,只需手动转到URL并输入参数。
    http://todolist.local/teachers/search?text=a
如果您有任何回应。这意味着您的PHP运行良好。

如果遇到错误,则说明您使用的是GET方法。请在Route中传递变量参数

Route::get('/search/{searchName}', 'TeachersController@ajaxsearch');

,请更正您的控制器

public function ajaxsearch($searchname){
   if($searchname != ""){
       $teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname.'%' )->paginate(10);
       return response()->json($teacher);
   }
}

第二,请在jQuery中使用$ .get方法获取GET方法AJAX

<script>
    $(document).ready(function(){
        $('#searchname').on('keyup', function(){
           var text = $('#searchname').val();
            $.get(urlHere, function(response){
                console.log(response);
            });
        });
     });
</script>

查看控制台标签,如果得到响应