为什么 GET ajax 调用返回错误代码 500?

因此,我的页面包含一个项目列表和一个搜索栏,该搜索栏根据插入的文本动态搜索所有项目。我在输入上放置了一个 eventHandler,以便每次输入更改时进行 ajax 调用以进行过滤。


从另一个角度来看,我尝试实现相同的逻辑,但在复选框上使用标签。起初,我只是放置了警报以确保我的 JavaScript 正确接收所有标签,到目前为止没有任何问题。然后我尝试进行另一个 ajax 调用并记录结果,看看他的输出是否是我想要的。这次我收到了“GET link 500(内部服务器错误)”。


我使用 Laravel 8。


这是复选框对应的视图:


    @foreach($categories as $category)

                    <li class="list-group-item">

                        <div class="form-check">

                            <input class="form-check-input filtercheckbox" type="checkbox" name="checkboxfilter" id="{{$category}}check" value="{{$category}}">

                            <label class="form-check-label" for="inlineRadio1">{{strtoupper($category[0]).substr($category,1)}}</label>

                        </div>

                    </li>

                    @endforeach

我的JavaScript:


function filterItems(){

    var filter = [];

    for(var i=0; i<checkbox.length; i++) {

        if(checkbox[i].checked){

            filter.push(checkbox[i].value);

        }

    }


    $.ajax({

        url:"/aluno/shop/filter",

        method:"GET",

        data:{ filter:filter },

        dataType:"json",

        success:function (data) {

            console.log(data);

            //document.getElementById('items_list').innerHTML = data;

        }

        }

    );

}

var checkbox = document.getElementsByClassName("filtercheckbox");


for(var i=0; i<checkbox.length; i++) {

    checkbox[i].addEventListener("change",filterItems);

}

我的功能控制器:


public function filterCategory(Request $request){

        if($request->ajax()){

            $query = $request->get('filter');

            $materials = array();


            if( $query != ''){

                foreach ($query as $cat){

                    $materialsList = Material::where('category','=', $cat)->get();

                    array_push($materials,$materialsList);

                }

            }else{

                $materialsList = Material::all();

            }

由于 dd() 函数在这种情况下不起作用,因此我无法判断问题是什么,是否有调用、响应或两者之间的任何内容。感谢一些帮助,我还可以展示我如何进行有关文本搜索的其他 ajax 调用,不知道是否可能有任何冲突。


慕村225694
浏览 68回答 1
1回答

慕田峪7331174

明显的问题出在$materials->count() > 0if 语句中。$materials 作为数组启动,并且->count()不存在于数组中。更改if($materials->count() > 0)为if(count($materials) > 0),它应该可以工作public function filterCategory(Request $request){&nbsp; &nbsp; &nbsp; &nbsp; if($request->ajax()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = $request->get('filter');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $materials = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $query != ''){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($query as $cat){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $materialsList = Material::where('category','=', $cat)->get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($materials,$materialsList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $materialsList = Material::all();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count($materials) > 0){&nbsp; &nbsp; //Changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($materialsList as $material){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output .= "<div class='col'>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<div class='card mt-2'>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<img class='shopImage' src='" . $material['image'] .&nbsp; "' alt='product'/>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<div class='card-body'>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<h5 class='card-title'>" . $material['name'] . "</h5>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</div>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<ul class='list-group list-group-flush'>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<li class='list-group-item'>Quantidade:" .&nbsp; $material['amount'] . "</li>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<li class='list-group-item'>Categoria:" . strtoupper($material['category'][0]).substr($material['category'],1) . "</li>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<li class='list-group-item'><a href='" . route('edit_item',$material['id']) . "' class='btn btn-primary'>Editar</a></li>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</ul>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</div>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</div>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output = 'Item com esse nome não encontrado!';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = $output;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo json_encode($data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript