Laravel AJAX获取请求返回500(内部服务器错误)

时间:2020-05-02 15:45:25

标签: javascript php jquery ajax laravel

我正在尝试使用AJAX进行搜索,但是我不断收到内部服务器错误。

这是我的文档就绪代码。

<scroller>
  <scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
</scroller>

该路线在路线组(LaraCast)下。

$(document).ready(function(){

                fetch_ads_data();
                function fetch_ads_data(query = ''){
                    $.ajax({
                        url:"{{route('adSearch.action')}}",
                        method: 'GET',
                        data:{query:query},
                        dataType:'json',
                        success:function(data){
                            $('#total_ads').html(data.total_ads);
                        }
                    });
                }
                $(document).on('keyup', '#search', function(){
                    var query = $(this).val();
                    fetch_ads_data(query);
                });
            });

这是控制器内部的功能。

Route::get('/AdsController/action', 'AdsController@action')->name('adSearch.action');

一直出现在控制台中的错误是:

public function action(Request $request){
    if($request->ajax()){
        $query = $request->get('query');
        if($query != ''){
            $data = Ad::orderBy('created_at', 'desc')->where('title','like', '%'.$query.'%');
        }else{
            $data = Ad::orderBy('created_at', 'desc');
        }
        $total_ads = $data->count();
        if($total_ads > 0){
            foreach($data as $ad){
                $output .= '
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="featured-box">
                        <figure>
                            <div class="icon">
                                <i class="lni-heart"></i>
                            </div>
                            <a href="#"><img class="img-fluid" alt=""></a>
                        </figure>
                        <div class="feature-content">
                            <div class="product">
                                <a href="#"><i class="lni-folder"></i> Real Estate</a>
                            </div>
                            <h4><a href="/ads/'.$ad->id.'">'.$ad->title.'</a></h4>
                            <span>Last Updated: '.$ad->updated_at.'</span>
                            <ul class="address">
                                <li>
                                <a href="#"><i class="lni-map-marker"></i> Dallas, Washington</a>
                                </li>
                                <li>
                                <a href="#"><i class="lni-alarm-clock"></i> '.$ad->created_at.'</a>
                                </li>
                                <li>
                                <a href="#"><i class="lni-user"></i> John Smith</a>
                                </li>
                                <li>
                                <a href="#"><i class="lni-package"></i> Used</a>
                                </li>
                            </ul>
                            <div class="listing-bottom">
                                <h3 class="price float-left"> ' .$ad->price. '</h3>
                            </div>
                        </div>
                    </div>
                </div>
                ';
            }
        }else{
            $output .= '
                <p>No Ads Found</p>
            ';
        }
        $data = array(
            'total_ads' => $output
        );

        echo json_encode($data);
    }
}

1 个答案:

答案 0 :(得分:0)

There are two string operators in PHP。第一个是并置运算符('.'),它返回左右参数的并置。第二个是串联赋值运算符('.='),它将右边的参数附加到左边的参数。

$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"

在您的情况下,您使用(.=)运算符,这意味着您需要先声明该变量。

$output="";
if($total_ads > 0){
  $output.="...";
}