# Load More with AJAX

**URL:** <https://forum.getkirby.com/t/load-more-with-ajax/17949>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 20, 2020, 4:46am UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949 "2020-04-20T04:46:59Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 10, 2020, 3:47pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/22 "2020-11-10T15:47:07Z")

</div>

> [@iam\_timm](#):
>
> ```auto
> return [
> compact('offset', 'limit', 'interviews', 'more')
> ];
> 
> ```

This syntax is not correct, please remove the outer square brackets. You cannot wrap compact inside an array, either use compact use put key/value pairs into an array.

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 10, 2020, 3:50pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/23 "2020-11-10T15:50:18Z")

</div>

Oh yeah, that fixed the error, thanks.

I still have the issue that when I press the more button, nothing happens. No errors at all.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 10, 2020, 4:06pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/24 "2020-11-10T16:06:40Z")

</div>

> [@iam\_timm](#):
>
> `const url =`${element.data(‘page’)}.json`;`

These backticks shouldn’t be there either.

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 10, 2020, 4:12pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/25 "2020-11-10T16:12:00Z")

</div>

> [@iam\_timm](#):
>
> ${element.data(‘page’)}.json

I think those are required for ES6, when i remove them it throws and error.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 10, 2020, 4:53pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/26 "2020-11-10T16:53:26Z")

</div>

> [@iam\_timm](#):
>
> ` console.log(url);`

does it log the url to console when you click?

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 10, 2020, 5:45pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/27 "2020-11-10T17:45:51Z")

</div>

Yes, the URL is logged.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 10, 2020, 5:48pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/28 "2020-11-10T17:48:32Z")

</div>

Do you get anything back from the ajax request if you log `data` after `$.get`?

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 10, 2020, 6:27pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/29 "2020-11-10T18:27:34Z")

</div>

I don’t get anything back from that. The .json is empty, there should be something there right?

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 10, 2020, 6:28pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/30 "2020-11-10T18:28:43Z")

</div>

But you can open the url in the browser?

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 11, 2020, 6:57pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/32 "2020-11-11T18:57:15Z")

</div>

Yeah the url does navigate to a pages yes.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 11, 2020, 11:39pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/33 "2020-11-11T23:39:51Z")

</div>

What does that mean? If you open `https://yourdomain.test/interviews.json` you should not get a page but see some json data.

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 13, 2020, 10:46am UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/34 "2020-11-13T10:46:12Z")

</div>

Yeah, no data show up at all. Very confused.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 13, 2020, 11:20am UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/35 "2020-11-13T11:20:19Z")

</div>

What do you get under that URL? an empty page? An error message?

If the page is empty, maybe the snippet doesn’t return anything. Test

```auto
html .= 'Some string here';

```

instead of the snippet to see if this returns anything.

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 13, 2020, 11:40am UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/36 "2020-11-13T11:40:20Z")

</div>

Yeah, this doesn’t work. Its seems like that file is not compiling any PHP at all.

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/6/6a7e59a68e15d0a34920ff2f6db7b42ee968a3f2.jpeg)

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 13, 2020, 12:05pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/37 "2020-11-13T12:05:12Z")

</div>

Hm, looks like `$interviews` is empty. Try to store `dump($interviews)` in the `$html` outside the loop.

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 13, 2020, 12:30pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/38 "2020-11-13T12:30:12Z")

</div>

```
<pre>Kirby\Cms\Pages Object
(
)
</pre>
```

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 13, 2020, 12:31pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/39 "2020-11-13T12:31:43Z")

</div>

Could you please post your current json controller again ( as code, not as screenshot)?

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 13, 2020, 12:32pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/40 "2020-11-13T12:32:59Z")

</div>

```
<?php

return function ($pages, $page) {
  
  $interviews = $pages->find('interviews')->children()->flip()->listed();
  $count = $interviews->count();

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

  $interviews = $interviews->offset($offset)->limit($limit);

  $more = $count > $offset + $limit;

  return compact('offset', 'limit', 'interviews', 'more');
};
```

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 13, 2020, 12:47pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/41 "2020-11-13T12:47:10Z")

</div>

> [@iam\_timm](#):
>
> ```auto
> $offset = intval(get('offset'));
> $limit = intval(get('limit'));
> 
> ```

Ah, ok, since at this time (before the Javascript kicks in) these values are not set, `$interviews` will be empty. So this is normal behavior.

Once you call the URL via JS though, the request will change and these values will be set.

For testing, you can assign

```auto
  $offset = 0;// intval(get('offset'));
  $limit = 2;// intval(get('limit'));

```

and your URL should then return something (undo this change again afterwards).

---

<div class="post-metadata">

**Author:** ![iam\_timm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/iam_timm/32/4728_2.png) [@iam\_timm](https://forum.getkirby.com/u/iam_timm)\
**Post date:** [November 13, 2020, 12:47pm UTC](https://forum.getkirby.com/t/load-more-with-ajax/17949/42 "2020-11-13T12:47:55Z")

</div>

Yeah this works now.

[Previous page](https://forum.getkirby.com/t/load-more-with-ajax/17949.md?page=1)

[Next page](https://forum.getkirby.com/t/load-more-with-ajax/17949.md?page=3)
