Ajax Calls get no response


#1

Hi, I have now managed the upgrade to cakephp “3.6.*” and the application is running fine, exept one feature. I have implemented Ajax Calls based on the sandbox examples von Dereuromark.
I tried to find a hint in the documentation, but there is nothing mentioned related to cake versions.
After checking the code, I see no error, so my more general question is:
Is there a difference using this feature in a newer cake version?

The ajax-call is working fine and the needed data are collected, but the response is ‘undefined’. The view in the ajax-folder is not starting.

**account_costs_ajax.ctp:**
<?php
$confSelect =$this->get('appConfig');

if ($costcenters->toArray()) {
	echo '<option value="">' . $confSelect['defaultBefore'] . __('KDWpleaseSelect') . $confSelect['defaultAfter'] . '</option>';
	foreach ($costcenters as $k => $v) {
		echo '<option value="' . $k . '">' . h($v) . '</option>';
	}
} else {
	echo '<option value="0">' . $confSelect['naBefore'] . __('KDWnoOptionAvailable') . $confSelect['naAfter'] . '</option>';
}



**Controller-Function:**
    public function accountCostsAjax() {
        //$this->autorender -> false;
        $this->request->allowMethod('ajax');
        $id = $this->request->query('id');
        $this->log("ID " . $id);
        $kontotype = $this->divTools->getAccountType($id);
        $this->set('kontotype', $kontotype);
        $this->log("Kontotyp " . $kontotype);
        if ($kontotype < 3) {
            if (!$id) {
                throw new NotFoundException();
            }

            $this->viewClass = 'Ajax.Ajax';
            $this->loadModel('Costcenters');

            $costcenters = $this->Costcenters->getListByAccount($id);
            foreach ($costcenters as $key => $value) {
                $this->log('[value]'. $value);
            }

            if (empty($costcenters)) {
                  $this->log("No Subaccounts");
            }

            $this->set(compact('costcenters'));
        }
    }



**js-script starting ajax call:**

    $(function () {
        $('#passive').change(function () {
            var selectedValue = $(this).val();
            var targeturl = $(this).attr('rel') + '?id=' + selectedValue;
             console.log(targeturl);
            $.ajax({
                type: 'get',
                url: targeturl,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                },
                success: function (response) {
                    console.log('response '+response.content);
                    if (response.content) {
                        console.log(response.content);
                        $('#costcenter').html(response.content);
                        if(response.content == '<option value=\"0\"> -- noOptionAvailable -- </option>') {
                             $('#extension').html(response.content);
                        }
                    }
                },
                error: function (e) {
                    alert("Unterkonto nicht gefunden: " + e.responseText.message);
                    console.log(e);
                }
            });

        });

    });

#2

Hi Klaus,
just a wild guess . I still use 2.6.x version, but in any case:
In 2.x you need
Router::mapResources(‘utills’);
Router::parseExtensions(‘json’, ‘csv’, ‘xml’); - in router.php

and public $components = array(‘RequestHandler’); in controller.
Without it , I think you get whole page with layout. Easy to check to comment $this->request->allowMethod(‘ajax’); and make request in browser - you will see how looks your data.

But, may be my idea is incorect.


#3

I have created a gist for you, what is a working ajax example. Please take a look.