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

问题描述:

我正在使用Laravel框架。我被卡住了。我成功获取数据。但是,当我想显示数据时,它会以数组形式显示我的数据。任何人都可以帮助我如何显示清单。我有一个布局内div如何在div中显示AJAX响应数据

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

enter image description here

+0

它看起来像你的服务器正在返回信息作为一个数组。您可能需要遍历响应并构建您的html。 –

+0

你想要的输出是什么? –

+0

我想在布局 – kunal

Iguess那你想返回布局+数据,所以你可以使用:

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

inste广告作者:

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

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

希望这会有所帮助。

的问题是这样的:

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

相反的print_r,返回数据为JSON,以便它可以通过jQuery的解析:

return response()->json($allData); 

然后在你的jQuery成功功能,您可以遍历数据

success:function(allData) 
{ 
    // simple example showing the firstnames 
    var example = ''; 
    $.each(allData, function(){ 
     example += ', ' + this.firstname; 
    }); 
    $("#ajaxListings").text(example); 
}, 
+0

显示Html数据不工作MrCode它显示空白屏幕:( – kunal

+0

其显示只有我的名字如何显示我的HTML数据 – kunal