What I want to do is to return json from my controller, this is my controller:
class BooksController extends Controller{
public function getIndex(){
return View::make('books.index');
}
public function getBooks(){
$books = array('Alice in Wonderland','Tom Sawyer','Gulliver\'s Travels','Dracula','Leaves of Grass');
return Response::json($books);
}
}
and this is my route:
Route::controller('books', 'BooksController');
and this is my view:
<html>
<head>
<meta charset=utf-8 />
<title>Show Books</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<a href="#" id="book-button">Load Books</a>
<div id="book-list"></div>
<script>
$(function() {
$('#book-button').on('click', function(e) {e.preventDefault();
$('#book-list').html('loading...');
$.get('books/books', function(data) {var book_list = '';
$.each(data, function(){
book_list += this + '<br>';
})
$("#book-list").html(book_list);
$('#book-button').hide();
});
});
});
</script>
</body>
</html>
when I load the view and I click the "Load Books" button the javascript just showing "loading...", no data from the controller is appear. what's wrong with my code?, help me guys. thanks