I have looked a lot through this StackExchange but I guess I am not too advanced in using AJAX/Json.
My aim is to have page transitions using Ajax. This was rather easy to accomplish following this article: https://designbycosmic.com/journal/craft-cms-ajax-page-transitions-with-history-pushstate
Now what this article is missing out is the updating of the page title and meta tags. I have done a lot of searches and from what I understand I need to build json files for each page/entry.
So I installed ElementAPI and tried to get the basics working
<?php
namespace Craft;
return [
'endpoints' => [
'news.json' => [
'elementType' => 'Entry',
'criteria' => ['section' => 'news'],
'transformer' => function(EntryModel $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'jsonUrl' => UrlHelper::getUrl("news/{$entry->id}.json")
];
},
]
]
];
But I am getting stuck at this point. I know that I can get the jquery through
$.ajax({
type: 'get',
url: 'news.json',
data: {
format: 'json'
},
success: function(result){
$main.html(result);
},
error: function(){
console.log("error.");
location.reload();
}
});
But if I do that I am getting an empty body that loads nothing.
I kind of am stuck here as my knowledge ends.
I would be very grateful for any information on how to progress. I am really looking to understand how this works but without having at least a working model it's hard for me to analyze and reverse-engineer the code.