简体   繁体   English

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

[英]Laravel AJAX get request return 500 (Internal Server Error)

I am trying to make a search bar with AJAX, but I keep getting Internal Server Error.我正在尝试使用 AJAX 创建一个搜索栏,但我不断收到内部服务器错误。

This is my Document Ready code.这是我的文档就绪代码。

$(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);
                });
            });

The Route is under a route group (LaraCast). Route 位于路由组 (LaraCast) 下。

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

This is the function inside the controller.这是 controller 内的 function。

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);
    }
}

The error that keeps appearing in the console is:控制台中不断出现的错误是:

app.js:1 GET http://localhost:8000/ar/AdsController/action?query= 500
(Internal Server Error)
send @ app.js:1
ajax @ app.js:1
fetch_ads_data @ ads:312
(anonymous) @ ads:310
j @ jquery-min.js:2
fireWith @ jquery-min.js:2
ready @ jquery-min.js:2
I @ jquery-min.js:2

There are two string operators in PHP . PHP 中有两个字符串运算符 The first is the concatenation operator ('.') , which returns the concatenation of its right and left arguments.第一个是连接运算符('.') ,它返回其左右 arguments 的连接。 The second is the concatenating assignment operator ('.=') , which appends the argument on the right side to the argument on the left side.第二个是连接赋值运算符('.=') ,它将右侧的参数附加到左侧的参数。

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

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

in your case, you use (.=) operator, that's mean you need to declare the variable first.在您的情况下,您使用(.=)运算符,这意味着您需要先声明变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM