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

Phalcon controller response and Ajax call

Hello all. I have AJAX call:

function showResponse(responseText, statusText, xhr, form) {
            console.log(responseText);
            console.log(statusText);
            console.log(JSON.stringify(xhr));
        }

        $('form#saveConfig').on('submit', function() {
            var post = $.post('submitForm', {
               someData
            }, showResponse);
            post.error(function(data) {
                console.log('Error: ' + JSON.stringify(data));
            });
        });

And I have echo in controller:

public function submitForm() {
        $this->view->disable();
        echo json_encode('Ok');
        die;
    }

Controller getting data from POST, but AJAX doesn't receive answer from controller. Why?

I'm using MicroMVC.

EDIT: When I was used:

public function submitForm() {
        $this->response->setJsonContent('Hello')->send();
    }

in browser all ok, but AJAX doesn't receive Hello string

P.S. Sorry for my bad English =(



98.9k
Accepted
answer

Send must not be called explicitly, you just have to return the response object:

public function submitForm() {
        $this->response->setJsonContent('Hello')
        return $this->response;
}


4.2k

Thank you very much

Send must not be called explicitly, you just have to return the response object:

public function submitForm() {
       $this->response->setJsonContent('Hello')
       return $this->response;
}