# Missing template while using Ajax-Plugin from dereuromark

**URL:** <https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100>\
**Category:** Need Help\
**Created:** [July 6, 2024, 11:28am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100 "2024-07-06T11:28:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheMiller](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@TheMiller](https://discourse.cakephp.org/u/TheMiller)\
**Post date:** [July 6, 2024, 11:28am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/1 "2024-07-06T11:28:56Z")

</div>

Hello there,

I’m trying to use the Ajax plugin (dereuromark/cakephp-ajax). I’ve CakePHP 4.4.12 running and set up the plugin as mentioned in readme. When I make my Ajax call, I receive a json-formatted response which contains the keys “error”, “success” and “content”. The value of “error” is a html error page telling me, the template [controllername]/ajax/index.php is missing. But as I’m using the AjaxComponent such a template should not be necessary.

I would expect, that a json-response would automatically be created and returned if it is an Ajax request, otherwise, the “normal” view would be rendered.

Can sb. help me / give me advice?

Actual response:

```auto
{
    "error": {
        "xdebug_message": "<tr><th align='left' bgcolor='#f57900' colspan=\"5\"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Cake\\View\\Exception\\MissingTemplateException: Template file `Articles/ajax/index.php` could not be found.\n\nThe following paths were searched:\n............"
    }
    "content": null
    "success": false

```

routes.php

```auto
$routes->scope('/', function (RouteBuilder $routes) {

   Router::extensions(['json']);
   ...
}

```

Application.php

```auto
public function bootstrap(): void
{
        // Call parent to load bootstrap from files.
        parent::bootstrap();
        $this->addPlugin('Ajax', ['bootstrap' => true]);

```

AppController.php

```auto
public function initialize(): void
{
    parent::initialize();
    $this->loadComponent('Ajax.Ajax');
}

public function beforeRender(\Cake\Event\EventInterface $event)
{
    parent::beforeRender($event);

    if ($this->request->is('ajax')) {
          $this->viewBuilder()->setClassName('Ajax.Ajax');
    }
}

```

A Controller

```auto
public function viewClasses(): array {
        if (!$this->request->getParam('_ext')) {
            return [];
        }

        return [JsonView::class];
    }

public function index()
    {
        $this->request->allowMethod(['ajax', 'get']);

       
        $data = $this->paginate($this->Articles);

        //From sandbox "simple.php" (dereuromark)
        if ($this->request->is(['ajax'])) {
            // Lets create current datetime
            $now = date("d-m-Y-H-i-s");
            $this->set('result', ['now' => $now]);
            $this->set('_serialize', ['result']);
        }
        else
        {
             $this->set(compact('data'));
             $this->set('_serialize', ['data']);
        }
    }

```

view

```auto
function ajaxTest()
{
    $.ajax({
        url: '/articles/index.json',
        method: 'get',
        dataType: 'json',
        success: function(data) {
            alert(data);
        },
        error: function(xhr, status, error) {
            alert(error);
        }
    });
}

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![TheMiller](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@TheMiller](https://discourse.cakephp.org/u/TheMiller)\
**Post date:** [July 7, 2024, 7:39am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/2 "2024-07-07T07:39:09Z")

</div>

Hello, I made some progress. After stepping through AjaxView.php (from Plugin) I saw, that rendering will be skipped, when the var “success” is set. I tried by modifying my code like this:

```auto
        if ($this->request->is(['ajax'])) {
            // Lets create current datetime
            $now = date("d-m-Y-H-i-s");
            $this->set('result', ['now' => $now]);
            $this->set('success', true);
            $this->set('_serialize', ['success', 'result']);
        }

```

Now, the response seems ok, **but why I have to set “success” and how to handle errors?** I thought, the AjaxComponent of cakephp-ajax handles this for us. Did I get sth. wrong or has this behavior changed? Looking at the sandbox code ([cakephp-sandbox/plugins/Sandbox/src/Controller/AjaxExamplesController.php at 32b30a842bd1d6af078c928c1433c1d3be3b9d03 · dereuromark/cakephp-sandbox · GitHub](https://github.com/dereuromark/cakephp-sandbox/blob/32b30a842bd1d6af078c928c1433c1d3be3b9d03/plugins/Sandbox/src/Controller/AjaxExamplesController.php#L45)), there is no need for a success variable.

```auto
{
    "error": null,
    "success": true,
    "content": null,
    "_message": null,
    "result": {
        "now": "07-07-2024-07-34-35"
    }
}

```

---

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [July 7, 2024, 10:13am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/3 "2024-07-07T10:13:42Z")

</div>

try to return it as json type, are you tring to get data or submit data?

---

<div class="post-metadata">

**Author:** ![TheMiller](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@TheMiller](https://discourse.cakephp.org/u/TheMiller)\
**Post date:** [July 7, 2024, 11:30am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/4 "2024-07-07T11:30:51Z")

</div>

Thanks. I’m trying to “GET” data. I noticed, that when I render an element, it also works correctly. It’s just for the case, when I want to return “plain” data.

So this also works:

```auto
if ($this->request->is('ajax')) {
    $this->render('/element/Articles/test');
}

```

Output is json

```auto
_message: null
content: "<div class=\"row\">↵ ... <table>↵ …"
error: null
success: null

```

Then, just like you suggested, I tried to set the return type as json and also ajax but get the same error.

```auto
$this->response = $this->response->withType('json');
//or also
$this->response = $this->response->withType('ajax');

```

But despite of this specific return problem, the whole plugin works as expected and documented. Tested it with the “delete” action and corresponding documentation.

---

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [July 8, 2024, 1:06am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/5 "2024-07-08T01:06:41Z")

</div>

$result = [‘test’ =\> ‘data’];  
return $this-\>response-\>withType(‘application/json’)  
-\>withStringbody(json\_encode($result));

you can try this in your controller

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [July 8, 2024, 9:33am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/6 "2024-07-08T09:33:23Z")

</div>

In Cake 3.1, when you don’t want to use view/template file you can set  
`$this->autoRender = false`  
or maybe in your case when you have action named “index” but view named “ajaxTest” try to use `$this->render('ajaxTest');`

---

<div class="post-metadata">

**Author:** ![dereuromark](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dereuromark/32/37_2.png) [@dereuromark](https://discourse.cakephp.org/u/dereuromark)\
**Post date:** [July 13, 2024, 9:21am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/7 "2024-07-13T09:21:55Z")

</div>

If you directly return JSON data and don’t need templating, then don’t use the AJAX plugin  
The whole idea of the ajax plugin, as the sandbox and the plugin itself explains is to render templated elements more easily. For both normal and AJAX use case without having to create different ones.  
So in your case you should be able to directly use JSON view instead, that’s it, nothing else needed.

---

<div class="post-metadata">

**Author:** ![TheMiller](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@TheMiller](https://discourse.cakephp.org/u/TheMiller)\
**Post date:** [July 14, 2024, 8:50am UTC](https://discourse.cakephp.org/t/missing-template-while-using-ajax-plugin-from-dereuromark/12100/8 "2024-07-14T08:50:17Z")

</div>

Hello guys,

thanks for the explanation. I just was afraid of setting it up wrong. I read about the use cases for that plugin but I forgot it while playing with it.

Many thanks for your help.
