Kirby .json representation stopped working

Hi,
I’m working on a website (with Kirby 2.4.1) which makes use of the content representation in order to make a json request. The request already worked, but now it stopped working and I keep getting a 404-response. (I’m running a local server with MAMP on localhost:8888) Here is the scenario :

I want to make a json request to retrieve all the URLs of video files in the subpage videos of the about page. The content-folder structure is this :

1-about/
  about.txt
  videos/
    videos.txt (empty .txt file)
    videofile1.mp4
    videofile2.mp4
    ...
2-projects/
...

In site/templates/ I have a template file called videos.json.php :

<?php 

  $videos = array();
  foreach ($site->find('about')->find('videos')->files() as $file) {
    if ($file->type() == "video") {
      array_push($videos, $file->url());
    }
  }
  $videourl = $videos[array_rand($videos, 1)];

  echo json_encode($videourl); 
  
?>

Then with a jQuery Ajax I request the json from /about/videos.json with :

$.ajax({
	url: 'about/videos.json',
	type: 'GET',
	complete: function(jqXHR, status) {
		console.log(jqXHR, status);
	},
	success: function (result) {
		console.log(result);

		// do something on success...
	}
});

Like I wrote, the setup already worked, but for some reason, it stopped and the complete function of the Ajax request gives me a 404 response status. The basic .htaccess file is present.

Can anybody see, what’s wrong with this setup?

Thanks

Probably due to the relative URL…

Try:

url: window.location.href + 'about/video.json',

I tried replacing the relative path with the absolute URL window.location.href + 'about/videos.json', but I still receive the 404 response. To be more precise, the responseText of the jqXHR object, I receive from the Ajax request is:

"<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>Site Title | Error</title></head><body><h1>Error</h1></body></html>"

As I wrote, the request worked before (with the relative URL) and I was also able to get a proper response directly in the browser from localhost:8888/about/videos.json.

EDIT:

I tried my setup with a fresh Kirby Starterkit (2.5.6) and now the responseText to the request is "<h1>Default template</h1>" which is the code of my default.php template, the status is still 404. So somehow my json request seems to route to the default template now. I also tried adding dataType = "json" to the request, but this doesn’t change anything.

Have you added a videos.php template?

That’s it! Now it works.

I didn’t have a videos.php template as I need the content of the videos-folder only by json request. Placing an empty videos.php next to the videos.json.php solved it.

It’s not self-evident that you’d need this “empty” and I think it would be good to mention this in the Content Representations Docs.

In any case, the Content Representations are a great feature, thanks.

I have created an issue on GitHub, maybe there is a reason why the template has to exist. I will make a mental note to update the docs in any case.