Setting up a simple API per the cookbook and I’m getting a weird result:
Here’s my API code:
<?php
header('Content-type: application/json; charset=utf-8');
$data = $pages->find('podcasts')->children()->visible()->limit(5);
$json = array();
foreach($data as $entry) {
$json[] = array(
'url' => (string)$entry->url(),
'title' => (string)$entry->title(),
'date' => (string)$entry->date('F d, Y'),
'subtitle' => (string)$entry->subtitle(),
'audio' => (string)$entry->file(),
);
}
echo json_encode($json);
?>
And the API call:
<script>
// call our api
$.getJSON('http://localhost:8888/admin/api', function(r) {
// loop through the result
$.each(r, function(i, entry) {
//console.log(entry);
var episode;
episode += '<div class="player">';
episode += '<a href="javascript:;" class="play-button"><i class="fa fa-play-circle"></i></a>';
episode += '<a href="javascript:;" data-audio='+ entry['audio'] +'><h4 class="nomargin">'+ entry['title'] +'</h4></a>';
episode += '<p class="nomargin">'+ entry['date'] +'</p>';
episode += '<p class="mar-t0"><strong>'+ entry['subtitle'] +'</strong></p>';
episode += '</div>';
$('#player').append(episode);
});
});
</script>