Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 =(



81.1k
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;
}


2.2k

Thank you very much

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

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