To use the routes defined in setRoutes()
, we call them using the jQuery .ajax()
method as in the following code:
function findAll() { $.ajax({ type: 'GET', url: rootURL, dataType: "json", // data type of response success: renderMenu }); }
The preceding code is pretty much self-explanatory. The function findAll()
called from the $(document).ready
function loads the list of all the photos by sending the GET request to the /photos
route and renders them using the renderMenu
function. The PHP function getPhotoList()
assigned to this route, reads all photos from the database as an array of stdClass
objects, encodes them to JSON format, and sends them back using the following code:
public function getPhotoList() { $sql = "select * FROM photos ORDER BY location"; try { $stmt = self::$dbh->query($sql); $photos = $stmt->fetchAll(PDO::FETCH_OBJ); //var_dump($photos); echo '{"photo": ' . json_encode($photos) . '}'; //here we send JSON encoded...