JSON calls with limit & offset cause 500 error

Ok, a bit stumped here. Until recently, this worked.

On lucasvasilko .com, when you scroll to the end of the site, or click the “load more images” button, it sent an AJAX request for more images in JSON form, with limit and offset options, like this: http://lucasvasilko.com/projects/people.json?limit=16&offset=16

For some reason, now, that call is giving a 500 error. But, when you actually open that link, it does render the json — it’s just that it’s identical to the call without the limit and offset: http://lucasvasilko.com/projects/people.json

in the console, it says “An unexpected error occurred! Enable debug mode for more info”

I did this, and don’t see any updated info in the console.

So my guess is something is happening with the ability for kirby to process the ?limit=16&offset=16 now that wasn’t happening a year ago?

Even more curious, when i try to run the repo locally on MAMP, I only get a page reading “This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.” This also used to work, and the site and panel loads fine on the web — you can open lucasvasilko .com fine, it’s just the ajax calls that are failing.

the whole repo is on github under user warronbebster, repo name lucasvasilko. Won’t let me post this as a link because I’m a new forum user.

on kirby 2.5.8
server settings:

When I test this locally, I also get the error, due to an undefined $more variable. So that means you have to initialize the variable first.

The other problem is that there are only 12 files in the home folder, at least in the repo version of the website, so more will never be true (in all three controllers where you are using this code).

If I were you, instead of the r::ajax check, I’d create a separate controller for the content representation (like in our new Kirby 3 recipe: http://getkirbytest.test/docs/cookbook/templating/ajax-load-more#json-controller)

On a side note: The Kirby 2 version you are using is not the latest one, you might consider updating to the latest and last ever Kirby 2 version (because Kirby 2 has reached EOL), 2.5.14: Releases · getkirby-v2/kirby · GitHub

Thank you! that $more intitialization solved my problem running locally.

I get a 404 for that getkirbytest url. It seems likely that whatever is behind that link will solve my problem. Is there a typo in the url?

Oops, so sorry, I had my local version open :see_no_evil:

The URL should of course be

Following that tutorial, I’m still struggling with how to get a json object back from the json controller/template based on passing a limit and offset in the url parameters.

in site/controllers/project.php:

<?php

return function($site, $pages, $page) {
  $limit    = 16;

  $images = $page->images()->sortBy('sort', 'asc')->limit($limit);
  //the above line is what needs to grab all the children images
  
  return [
    'limit'       => $limit,
    'images'      => $images,  
  ];
};

in site/controllers/project.json.php:

<?php

return function ($page) {
  $limit = 16;
  $offset = 0;
  $images = $page->images()->sortBy('sort', 'asc')->offset($offset)->limit($limit);
  $count = $page->images()->count();
  $more = $count > $offset + 1; //are there more images to load?
  
  return [
      'images'   => $images,
      'more'     => $more,
      'html'     => '',
      'json'     => [],
    ];
};

in templates/project.json.php:

<?php

header("Access-Control-Allow-Origin: *");

foreach($images as $image) {
 $html .= snippet('image', compact('image'), true);
}

// add $html and $more to the $data array

$data['html'] = $html;
$data['more'] = $more;

echo json_encode($data);

this still returns a single element in the json, no matter what project url I put in, nor if it has a limit or offset:
for example, /projects/books.json, /projects/people.json, and /projects/books.json?limit=16&offset=10 all return the same thing:

{"html":"<div class=\"image_container\">\n\t<figure>\n\t\t<img src=\"http:\/\/localhost:8888\/lucasvasilko\/content\/vasilko-person-8.jpg\" alt=\"\" \/>\n\t\t<!-- Only show caption if the caption field isn't empty -->\n\t\t\t<\/figure>\n<\/div>\n<!-- <br> -->","more":false}

In the json controller, you have to get the offset and limit from the query string instead of hardcoding them:

$limit  = intval(get('limit'));
$offset = intval(get('offset'));

ah, get() wasn’t working for some reason so I ended up using

$offset = param('offset', 0);
$limit = param('limit', 16);

and that worked! ty so much for the help!