在laravel中使用ajax从控制器刀片文件传递搜索数据时遇到问题

在laravel中使用ajax从控制器刀片文件传递搜索数据时遇到问题,ajax,laravel-5,Ajax,Laravel 5,这是我在Admin controller中的控制器方法,该方法接收搜索输入,但我在将输出数据传递到视图文件时遇到问题 public function action(Request $request) { if($request->ajax()) { $students=Student::where('name','LIKE','%'.$request->search."%")->paginate(5); $data = $st

这是我在Admin controller中的控制器方法,该方法接收搜索输入,但我在将输出数据传递到视图文件时遇到问题

public function action(Request $request)
{
    if($request->ajax())
    {

        $students=Student::where('name','LIKE','%'.$request->search."%")->paginate(5);

        $data = $students->count();
        if($data > 0)
        {
            $output=$students;

        }
        else{
           $output=$students;
        }
        return view('search' compact('output'));

    }

}
以下是视图中的ajax文件(
search.blade.php


更新下面的源代码

AdminController.php

public function action(Request $request)
    {
        if ($request->ajax()) {
            // Convert to lowercase after trim the searched text
            $search_text = strtolower(trim($request->search));
            $students = Student::where('name','LIKE','%'.$search_text."%")->paginate(5);
            $data = $students->count();
            $output = array();
            if ($data > 0) {
                $output = $students->toArray();
            }
            // Return JSON response
            return response()->json($output);
        }
    }
<input type="text" id="search">

<div id="studentlist">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>            
        </tbody>
    </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">

    $('#search').on('keyup',function(){
        $value=$(this).val();
        $.ajax({
            type : 'get',
            url : '{{route('search.action')}}',
            data:{'search':$value},
            success:function(response){
                var studentListHtml = "";
                if (response) {
                    if (response.data && response.data.length > 0) {
                        $.each(response.data , function( index, value ) {
                            studentListHtml += "<tr><td>" + value.id + "</td>" + "<td>" + value.name + "</td></tr>"; 
                        });
                        $("#studentlist table tbody").empty().html(studentListHtml);
                    } else {
                        $("#studentlist table tbody").empty().html("<tr><td colspan='2'>No students found</td></tr>");
                    }
                }
            }
        });

    })

</script>

<script type="text/javascript">
    $.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
search.blade.php

public function action(Request $request)
    {
        if ($request->ajax()) {
            // Convert to lowercase after trim the searched text
            $search_text = strtolower(trim($request->search));
            $students = Student::where('name','LIKE','%'.$search_text."%")->paginate(5);
            $data = $students->count();
            $output = array();
            if ($data > 0) {
                $output = $students->toArray();
            }
            // Return JSON response
            return response()->json($output);
        }
    }
<input type="text" id="search">

<div id="studentlist">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>            
        </tbody>
    </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">

    $('#search').on('keyup',function(){
        $value=$(this).val();
        $.ajax({
            type : 'get',
            url : '{{route('search.action')}}',
            data:{'search':$value},
            success:function(response){
                var studentListHtml = "";
                if (response) {
                    if (response.data && response.data.length > 0) {
                        $.each(response.data , function( index, value ) {
                            studentListHtml += "<tr><td>" + value.id + "</td>" + "<td>" + value.name + "</td></tr>"; 
                        });
                        $("#studentlist table tbody").empty().html(studentListHtml);
                    } else {
                        $("#studentlist table tbody").empty().html("<tr><td colspan='2'>No students found</td></tr>");
                    }
                }
            }
        });

    })

</script>

<script type="text/javascript">
    $.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>

身份证件
名称
$('#search')。在('keyup',function()上{
$value=$(this.val();
$.ajax({
键入:“get”,
url:“{route('search.action')}}”,
数据:{'search':$value},
成功:功能(响应){
var studentListHtml=“”;
如果(答复){
if(response.data&&response.data.length>0){
$.each(response.data、函数(索引、值){
studentListHtml+=“”+value.id+“”+“”+value.name+“”;
});
$(“#studentlist table tbody”).empty().html(studentlist html);
}否则{
$(“#studentlist table tbody”).empty().html(“未找到学生”);
}
}
}
});
})
$.ajaxSetup({headers:{'csrftoken':{{{{csrf_token()}}}}});

您有没有收到任何错误?如果是,请更新。否,但数据未返回
view()
不返回HTML-您需要
render()
它。关于这一点,这里有很多问题。更常见且可能更好的方法是让控制器返回数据(如JSON),并让前端呈现该数据,如迭代结果数组并创建HTML插入DOM。这是否回答了您的问题?请再帮我一个忙…如何显示这些值