Q:

CakePHP 2 and jQuery don't show results in DIV

I'm trying to make auto refresh DIV with updated data.  It seems to be ok but it doesn't show any results on webpage. 
Thanks for help :)
Controller:
public function homeNewMessagesAjax() {
            $this->disableCache();

            $options = array('conditions' => array('Message.is_read' => '0'));
            $messages = $this->Message->find('count', $options);

            $this->set(compact('messages'));

    }

JS:
$(document).ready(function(){
setInterval(function(){
        var targetUrl = "/Messages/homeNewMessagesAjax.json";
        $.ajax({
            type: 'post',
            url: targetUrl,
            dataType: 'html',
            cache: false,
            beforeSend: function(xhr) {
                xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            },
            success: function(response) {
                if(response.content){
                    $("#headerMessages").html(response.content);
                }
            },
            error: function(e) {
                console.log(e);
            }
        });
    }, 10000);
}

JSON view:
if ($messages > 0) {
    //some info
} else {
    //some info
}

View with destination DIV:
echo "<div id=\"headerMessages\">";

echo "</div>";

A:

I did some similar thing a few days ago.  The following works for me:
Controller:
public function admin_add() {
    $this->request->onlyAllow('ajax');

    // some controller logic ...

    $task = $this->Task->findById( $this->Task->id );
    $this->set(compact('task'));
    $this->set('_serialize', array('task')); // generates json response without the need of a view file
}

JS:
$('form#TaskAdminIndexForm').on('submit', function(e){
    e.preventDefault();
    var targeturl = '/admin/tasks/add.json';
    $.ajax({
        type: 'post',
        url: targeturl,
        data:$('form').serialize(),
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        },
        success: function(response) {
            var task = response.task. Task;
            $("#tasjTitle").html(task.name);
        },
        error: function(e) {
            alert("An error occurred: " + e.responseText.message);
            console.log(e);
        }
    });   
}); 

