# Cakephp 3.8.8 jquery ajax missing template

**URL:** <https://discourse.cakephp.org/t/cakephp-3-8-8-jquery-ajax-missing-template/11058>\
**Category:** Need Help\
**Created:** [March 17, 2023, 9:44am UTC](https://discourse.cakephp.org/t/cakephp-3-8-8-jquery-ajax-missing-template/11058 "2023-03-17T09:44:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daroath](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daroath/32/2685_2.png) [@daroath](https://discourse.cakephp.org/u/daroath)\
**Post date:** [March 17, 2023, 9:44am UTC](https://discourse.cakephp.org/t/cakephp-3-8-8-jquery-ajax-missing-template/11058/1 "2023-03-17T09:44:13Z")

</div>

In Cakephp I’m trying to make jquery ajax as json request, but Cakephp always response with missing template as error below:

```auto
{
    "message": "Template file \/Commissions\/json\/history.ctp\u0022 is missing.",
    "url": "commissions\/get-users?%7B%22company_id%22:%22e615a47c-a210-4a13-b3a7-edaa71f%22%7D\u0026amp;_=1679045364774",
    "code": 500,
    "file": "Tele\/TeleCommissions\/json\/history.ctp",
    "line": 1583
}

```

and here is my code. Any help appreciate.

```auto
public function getUsers()
	{
		if ($this->request->is('ajax')) {
			$this->autoRender = false;
			$this->response = $this->response->withDisabledCache();

			$this->loadModel('Users');
			$users = $this->Users->find('list', [
				'keyField' => 'id',
				'valueField' => 'username'
			])
			->where([
				'Users.is_active' => 1, 
				'Users.is_deleted' => 0, 
				'Users.type' => MARKETING,
				'Users.company_id' => $this->request->getQuery('company_id')
			]);
			return $this->response->withType('application/json')
				->withStringBody(json_encode([
					'status' => 1,
					'data' => $users,
				]));
		}
	}

$('body').on('change', '#company-id', function (e) {
			e.preventDefault();
			let companyId = $(this).val();
			console.log(companyId);
			$.ajax({
				type: 'GET',
				url: '<?= $this->Url->build(['controller' => 'Commissions', 'action' => 'getUsers']) ?>',
				data: JSON.stringify({ company_id: companyId }),
				contentType: 'application/json; charset=utf-8',
    			dataType: 'json',
				cache: false,
				beforeSend: function (xhr) {
					xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
				}
			}).done(function(data) {
				if (data) {
					let option = '<option value="">Select Users</option>';
					$.each(data.data, function(index, value) {
						option += '<option value="' + index + '">' + value + '</option>';						
					});
					$('.user-id').empty().html(option);
					<?php if ($this->request->getQuery('user_id')): ?>
						$('.user-id').val('<?= $this->request->getQuery('user_id') ?>');
					<?php endif; ?>
				}
			});
		});

```

---

<div class="post-metadata">

**Author:** ![Kaiser85](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kaiser85/32/2714_2.png) [@Kaiser85](https://discourse.cakephp.org/u/Kaiser85)\
**Post date:** [May 5, 2023, 3:42pm UTC](https://discourse.cakephp.org/t/cakephp-3-8-8-jquery-ajax-missing-template/11058/2 "2023-05-05T15:42:13Z")

</div>

Maybe instead of using:

```auto
return $this->response->withType('application/json')
				->withStringBody(json_encode([
					'status' => 1,
					'data' => $users,
				]));

```

use:

```auto
$this->set('data', ['status' => 1, 'data' => $users]);
$this->viewBuilder()->setOption('serialize', ['data']);

```
