views:

129

answers:

2

Hi,

I'm using jQuery to make an AJAX request to some controller action. This request is activated from a button in the view of the edit action in the same controller.

My problem: The Ajax Request is returning all the code of the edit view (with all the forms and inputs) instead of the expected number. If I put the same ajax button in the view of the add action, it works perfect (it returns the number).

The edit and add actions remains as generated by default (with bake).

This is the jQuery function to make the ajax request

        $.ajax({
            type: 'POST',
            url: 'checkTargets',
            data: {target: tgt_array, channel: channel_type},
            success:function(data){
                $('#num_subscribers > span').html(data);
            },
            error:function(){
                $('#num_subscribers > span').html("The subscribers could not be loaded");
            },
            timeout: 5000
        });
    } 

This is the action

function checkTargets() {
        if ($this->RequestHandler->isAjax()) { 
            if(!empty($this->params['form'])) {
                $data = $this->params['form'];

                if ($data['channel'] === 'SMS') {
                    $channel = 'sms';
                } else {
                    $channel = 'pin';
                }

                $targets = $this->processPostTargets($data['target']);
                $this->RequestHandler->respondAs('text');
                //This echo a NUMBER
                echo ClassRegistry::init('Selection')->countSubscribersInTarget($channel, $targets);

                Configure:: write('debug', 0);
                $this->autoRender = false;
                exit();

            }
        } 

    }

Why is this happening?

Thanks

A: 

Just a suggestion without having access to everything, have you tried this?

$this->autoRender = false;
$this->layout = 'ajax';

Also I'd suggest keeping it simple:

$.post("/controller/checkTargets", function(data) {
   alert(data);
}

function checkTargets() {
  $this->autoRender = false;
  $this->layout = 'ajax';
  echo "Im working";
}

and go from there.

xiaohouzi79
thanks xiaohouzi. I tried with that, but i figure out that the problem could be the URL param. I changed 'checktargets' for '/myapp/campaigns/checkTargets' and it's working.
Jose S
@Jose Rather than leaving your question hanging, I suggest adding your answer and marking it as your preferred answer (by clicking on the tick). Saves people from thinking you are still waiting for an answer.
xiaohouzi79
Yes, i will do that. I wanted to be sure the problem was solved. Thanks again
Jose S
A: 

I fixed the problem. It was the URL param in the .ajax function (a noob mistake)

It should be a complete path to the referenced action in the controller.

This question helped me to understand the problem: http://stackoverflow.com/questions/3352350/best-practice-to-use-action-url-while-calling-ajax-in-cakephp

Fixed Ajax Request:

    $.ajax({
        type: 'POST',
        url: '/myapp/campaigns/checkTargets',
        data: {target: tgt_array, channel: channel_type},
        success:function(data){
            $('#num_subscribers > span').html(data);
        },
        error:function(){
            $('#num_subscribers > span').html("The subscribers could not be loaded");
        },
        timeout: 5000
    });

thanks and excuse my english

Jose S