# Return json from controller

**URL:** <https://forum.getkirby.com/t/return-json-from-controller/20145>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 25, 2020, 9:18am UTC](https://forum.getkirby.com/t/return-json-from-controller/20145 "2020-11-25T09:18:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![caplod](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/caplod/32/5211_2.png) [@caplod](https://forum.getkirby.com/u/caplod)\
**Post date:** [November 25, 2020, 9:18am UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/1 "2020-11-25T09:18:26Z")

</div>

I have a controller where I want to render the template if it is a get request and if it is a post request I want to return json.  
On the page is a form that does a post via xhr.

When I test this on my local machine it returns json, but on production it returns the template.  
Any ideas why?

site/controllers/game.php

```
return function($page, $pages, $site, $kirby) {
  if($kirby->request()->is('POST')) {
    // do stuff
    echo json_encode($response);

  } else {
    // prepare some output
    return $output;
  }

};

```

---

<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 25, 2020, 9:42am UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/2 "2020-11-25T09:42:43Z")

</div>

The purpose of having a controller is that it returns a **data array** to your template. You should not echo something from your controller.

---

<div class="post-metadata">

**Author:** ![caplod](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/caplod/32/5211_2.png) [@caplod](https://forum.getkirby.com/u/caplod)\
**Post date:** [November 25, 2020, 2:18pm UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/3 "2020-11-25T14:18:06Z")

</div>

Well I want to return a data array (json) , but not render the template if it is a post request.

---

<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 25, 2020, 6:21pm UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/4 "2020-11-25T18:21:48Z")

</div>

Does your POST request come from an AJAX request?

---

<div class="post-metadata">

**Author:** ![caplod](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/caplod/32/5211_2.png) [@caplod](https://forum.getkirby.com/u/caplod)\
**Post date:** [November 25, 2020, 9:16pm UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/5 "2020-11-25T21:16:07Z")

</div>

Yes, I am using ‘fetch’

> **[Using Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)**
>
> This kind of functionality was previously achieved using XMLHttpRequest. Fetch provides a better alternative that can be easily used by other technologies such as Service Workers. Fetch also provides a single logical place to define other...

---

<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 25, 2020, 9:51pm UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/6 "2020-11-25T21:51:03Z")

</div>

Then it would make sense to use a json content representation template for the post request with its own controller if necessary.

> **[Content Representations](https://getkirby.com/docs/guide/templates/content-representations)**

In any case the template should echo the data, not the controller.

---

<div class="post-metadata">

**Author:** ![caplod](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/caplod/32/5211_2.png) [@caplod](https://forum.getkirby.com/u/caplod)\
**Post date:** [November 26, 2020, 7:54am UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/7 "2020-11-26T07:54:53Z")

</div>

Thank you very much! The content representation template works perfect.

One question though:  
is there a kirby way to get a link to the json version of a page?

```
$page->url() . '.json'

```

seem a little bit hacky.

---

<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 26, 2020, 8:01am UTC](https://forum.getkirby.com/t/return-json-from-controller/20145/8 "2020-11-26T08:01:56Z")

</div>

> [@caplod](#):
>
> is there a kirby way to get a link to the json version of a page?

No, there isn’t. If you need this more often, you could create a custom `jsonurl()` method.
