Using Ajax to render elements

Hello,
I am developing a functionality for a blog, and I would like to send an ajax request. From debugging I can see my request gets sent to my controller ,and the SQL gets set, but I don’t see any response get set with ‘compact’.
Can anyone help me? I don’t see any response.

My understanding is that from a request, cakephp will render, a view, and to have elements dynamically displayed, you need a null check if statement. Now I’ve done that, so to render that element I must set a variable with compact in the controller method. I’ve worked with ajax before with J2EE, but with cakephp I am not following its flow at all.

First of all If you want answer which exactly solve your problem, you should share your code that you tried so far. Theoretical questions are not so much helpful for the others if some issues you are facing. Btw you can go through http://stackoverflow.com/questions/30024565/simple-ajax-form-in-cakephp-3-0.

   if ($this->request->is('ajax')) {
          
        $some = TableRegistry::get('Articles');
        /* @var $z type */
        $d = $some->find('all')
                ->where(function ($exp, $q) {
                    return $exp->between('created', new DateTime(), new DateTime('2016-12-31'));
                })
                ->order(['created' => 'DESC'])
                ->limit(5);
        $this->set(compact('d'));
        $this->render('/Element/ajaxArticle');

Is my controller code, and in my view I have an element that is only displays if it is not null. I can see in my debug console that the variable is 1, when I use print_r. But in the cake debug tool kit my variable never shows up, but I can see the query string when I look in the browser console.

Thank you

Please follow this https://book.cakephp.org/3.0/en/views/json-and-xml-views.html. You have to use json_encode then you will get the data in Ajax response.
Also you can go through http://stackoverflow.com/questions/38169410/cant-get-a-json-response-from-cakephp-3-controller

Any reason why this database call would return an empty array? Something is still off with this.

If database returning an empty array then there will be some issue with your data not with the Ajax. Try to debug your query separately and see what happens.

I’ve debug my statement, and it says

2017-01-16 20:51:05 Debug: Cake\ORM\Query Object
(
[(help)] => This is a Query object, to get the results execute or iterate it.

There is more information. To me having this shows that I should be able to dynamically render elements in a view. But the ORM is not executing it seems?

Try to use before set $d = $d->toArray();.