2015-07-20 56 views
0

我發展與Laravel 5.1的應用程序,我試圖從json的信息,但我不能,我有下面的代碼:如何從Laravel獲得JSON 5.1

路線:

Route::get('toys','[email protected]'); 

控制器:

public function listing(){ 
     $toys = Toy::all(); 
     return response()->json([ 
     "toys" => $toys->toArray() 
     ]); 
    } 

信訪得到:

<script> 
     $(document).ready(function(){ 

     $.getJSON("http://localhost:8000/toys", function(data){ 

     }); 
    }); 
    </script> 

我可以看到數據進入控制檯:

但是如何顯示信息到HTML?感謝

+1

你應該學習一些JS/jQuery的有關如何操作DOM。或者你不應該使用json,只需使用html。 – worldask

回答

3

使用需要使用.接線員給每個對象的訪問數據,因爲你有對象的數組,你通過每個對象需要循環並訪問他們作爲下面的例子:

data.id - 用於訪問ID
data.genre - 用於訪問所述流派等

則可以通過jquery的數據對象作爲下面的示例環

$(data).each(function (key, value)) 
{ 
     $("#iddiv").append(value.id+","); //this will show all the ids in the div with id iddiv, which are separated by commas 
} 

<body> 
     <div id= "iddiv" > </div> 
</body> 

編輯:

這是一個二維的對象,這樣做下面的事情

$(data).each(function (key, value)) 
{ 
     $(value).each(function(key2, value2)){ 

     $("#iddiv").append(value2.id+","); //this will show all the ids in the div with id iddiv, which are separated by commas 

     }) 
} 
+0

好吧,我做了,但只顯示未定義,謝謝你的回答:) – Lagul

+1

你能告訴我究竟是什麼顯示undefined?你把上面的代碼放在你的'function(data){..}'部分代碼中了嗎? –

+0

在我

秀不確定, – Lagul