2016-11-10 96 views
0

JSON響應,這是我的控制器:如何返回laravel

public function index(Request $request) 
    { 

     $items = Post::latest()->paginate(5); 
     $cmnt=Comment::all(); 

     return response()->json(array('posts'=>$items,'comment'=>$cmnt)); 
    } 

**這是我的Ajax請求**

function getPageData() { 
     $.ajax({ 
     dataType: 'json', 
     url: "{{route('post_status.index')}}", 
     data: {page:1} 
     }).done(function(data){ 

     manageRow(data.data); 
     }); 
    } 
function manageRow(data) { 
      console.log(data.comment); 
} 

爲什麼我收到錯誤? 幫我出這個plzz

+0

「爲什麼我收到錯誤」 __What__錯誤? – tkausl

+0

實際上它沒有顯示任何內容.....沒有任何價值 – leo

+0

@leo你有沒有試過在控制檯上看?同樣在你對評論的回答中,你會談論多個對象?你能真正解釋你在你的問題中想做什麼嗎? –

回答

1

laravel默認返回JSON的,如果它不返回來看,你的情況指數()應該返回:

return ['posts' => $items,'comment' => $cmnt]; 

也是我不認爲這是正確的

{{route('post_status.index')}} 

大概應該是

{{ url('post_status/index') }} 
+0

路由是好的,當我返回一個對象時效果很好,但是當我想返回多個對象時它不起作用 – leo