我正在使用Laravel框架。我被卡住了。我成功获取数据。但是,当我要显示数据时,它将以数组形式显示数据。谁能帮我显示清单。我在div中有一个布局

<div class="col-md-9 col-sm-9  col-xs-12" id="ajaxListings">
    @include('layouts.publicLayout.get-listings')// here i have implemented layout
</div>




function filteredlistings(){
        $.ajax({
            url:'search-listings',
            data:{
                'service_name':title,
                'location':location
            },
            type:"get",
            success:function(allData)
            {
                $("#ajaxListings").html(allData);
            },
            error: function()
            {
                alert('error');
            }
        });
    }


这是我的功能:

public function search_listings(Request $request){
    if($request->ajax()){
        $data = $_GET;
        $users = DB::table('users')
        ->join('business_services', 'users.id', '=', 'business_services.user_id')
        ->join('listings','users.id', '=', 'listings.user_id')
        ->select('users.id', 'business_services.name', 'business_services.list_id','listings.location')
        ->where(['business_services.name'=>$data['service_name'],'users.service_name'=>"Seller"])
        ->where('listings.location','like','%'.$data['location'].'%')
        ->get();
        $users = json_decode(json_encode($users),true);
        foreach($users as $alluser){
            $ids[] = $alluser['id'];
        }
        $allData="";
        if(!empty($ids)){
                $allData = User::with('listings')->whereIn('id',$ids)->get();
                $allData = json_decode(json_encode($allData),true);
        }
        $title = "Nails";
        echo "<pre>"; print_r($allData); die;
    }
}


javascript - 如何在div中显示AJAX响应数据-LMLPHP

最佳答案

猜测您要返回布局+数据,因此可以使用:

return view('layouts.publicLayout.get-listings',compact('allData'));


代替 :

echo "<pre>"; print_r($allData); die;


您可以使用$allData访问布局内的数据。

希望这可以帮助。

关于javascript - 如何在div中显示AJAX响应数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40632735/

10-12 03:37
查看更多