We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Ajax response always empty when using foreign models in the called function...

Hi,

my problem : ajax calls return always an empty response when I use a model inside another one...example:

fooView:ajaxCall->fooController:function{ here I manipulate fooModel && barModel }:ajaxResponse[is empty].

I suppose that it's related to some headers modified when one controller deals with another one.. atm I'm splitting my request into multiples ones...



43.9k

Hi,

Please show:

  • view code with javascript
  • controller action


8.1k
class AjaxController extends ControllerBase
{

    protected $content;

    public function afterExecuteRoute()
    {
        $this->response->setJsonContent($this->content, JSON_NUMERIC_CHECK);
        $this->response->send();
    }

    public function someAction()
    {
        $data = [
            'name' => 'Sam',
            'data'   => 'data'
        ];
        $this->content = $data;
    }


11.6k

after more test, this happens only when I use ->create() or ->save()... all my others ajax tweaks work perfectly (I use, as suggested somewhere here, "afterExecuteRoute" in my controllerBase that is sorting ajax request from regular ones)..



43.9k

Hi,

are you sure that your ajax request are well formatted, that they are no routing problems ?



11.6k
edited May '15

edited (see my last post)



11.6k

hummm..reading doc about shared services...maybe I done something wrong in my config..Does controller should/can be declared as shared?



43.9k

so far I can see, all your actions above don't return any responses ...



11.6k

only my first function is returning params :

    ...
    $this->view->tarif = $tarifs[$tarif];
    $this->view->newArticle = $selectedArticle;   // work fine


43.9k

        if ($this->request->isPost()) {
            if ($this->request->isAjax()) {
                $paramBloc = json_decode($this->request->getPost('paramBloc'));
                $checkBlocExist = Blocarticle::findFirst(array(
                    'columns' => '*',
                    'conditions' => 'operationID = ?1 AND nomBloc = ?2',
                    'bind' =>
                        [
                            1 => $paramBloc->operationID,
                            2 => $paramBloc->nomBloc,
                        ]
                ));
                if(!$checkBlocExist) {
                    $blocArticle = new Blocarticle();
                    $blocArticle->operationID = $paramBloc->operationID;
                    $blocArticle->nomBloc = $paramBloc->nomBloc;
                    $blocArticle->save();
                    $newBloc = Blocarticle::findFirst(array(
                        'columns' => '*',
                        'conditions' => 'operationID = ?1 AND nomBloc = ?2',
                        'bind' =>
                            [
                                1 => $paramBloc->operationID,
                                2 => $paramBloc->nomBloc,
                            ]
                    ));
// after $blocArticle->save(); nothing is passed to the view
                }
                else {
// but here yes
                    $this->view->messageAddNewBloc = "bad";
                }
            }
        }


11.6k

aaargh... the way I explain things leads to confusion, sorry...so, makin it shorter/simple:

        public function createNewBlocAction() {
        if ($this->request->isPost()) {
            if ($this->request->isAjax()) {

                $paramBloc = json_decode($this->request->getPost('paramBloc'));

                $checkBlocExist = Blocarticle::findFirst(array(        //using here a foreign model (we are in InvoiceController.php)
                    'columns' => '*',
                    'conditions' => 'operationID = ?1 AND nomBloc = ?2',
                    'bind' =>
                        [
                            1 => $paramBloc->operationID,
                            2 => $paramBloc->nomBloc,
                        ]
                ));
                if(!$checkBlocExist) {
                    $blocArticle = new Blocarticle();
                    $blocArticle->operationID = $paramBloc->operationID;
                    $blocArticle->nomBloc = $paramBloc->nomBloc;
                    $blocArticle->save();

                    $this->view->messageAddNewBloc = "ok";  /// ----> doesn't work, ajax response always empty, and in Db, $blocArticle have been correctly saved
                }
                else {

                    $this->view->messageAddNewBloc = "bad";
                }

            }
        }
    }

ajax response : {"title":"Devis","success":true,"message":""}

the same, removing the 'save' op:

        public function createNewBlocAction() {
        if ($this->request->isPost()) {
            if ($this->request->isAjax()) {

                $paramBloc = json_decode($this->request->getPost('paramBloc'));

                $checkBlocExist = Blocarticle::findFirst(array(     //using here a foreign model (we are in InvoiceController.php)
                    'columns' => '*',
                    'conditions' => 'operationID = ?1 AND nomBloc = ?2',
                    'bind' =>
                        [
                            1 => $paramBloc->operationID,
                            2 => $paramBloc->nomBloc,
                        ]
                ));
                if(!$checkBlocExist) {
                    $blocArticle = new Blocarticle();
                    $blocArticle->operationID = $paramBloc->operationID;
                    $blocArticle->nomBloc = $paramBloc->nomBloc;
             //       $blocArticle->save();     we bypass the save operation

                    $this->view->messageAddNewBloc = "ok";  /// ----> is workin correctly, ajax response not empty
                }
                else {

                    $this->view->messageAddNewBloc = "bad";
                }

            }
        }
    }

ajax response : {"title":"Devis","messageAddNewBloc":"ok","success":true,"message":""}