wcolbert
wcolbert

Reputation: 2040

Ajax Response from CakePHP Controller returning null

I'm tryin to validate an input field with an ajax call to a cakephp controller My Ajax is:

$("#UserAlphaCode").change(function () {
        $.ajax({
            type: "post",
            url: '<?php echo $this->webroot ?>' + "/alpha_users/checkCode",
            data: ({code : $(this).val()}),
            dataType: "json",
            success: function(data){
                alert (data);
            },
            error: function(data){
                alert("epic fail");
            }
        });
    });

My controller code

function checkCode() {
        Configure::write('debug', 0);
        $this->autoRender = false;
        $codePassed = $this->params['form']['code'];
        $isCodeValid = $this->find('count',array('conditions'=> array('AlphaUser.code' => $codePassed)));
        if ($isCodeValid == 0){
            $codeResponse = false;
        } else {
            $codeResponse = true;
        }
        echo json_encode ($codeResponse);   
    }

I'm pretty sure I'm using $this->params wrong here to access the data sent from the ajax request. What should I be doing instead?

Upvotes: 0

Views: 2700

Answers (2)

L&#232;se majest&#233;
L&#232;se majest&#233;

Reputation: 8045

Don't know why it would be returning null, but I normally use $this->data to fetch form data.

And did you try debug($this->params)? If you don't have a non-AJAX form to test the request from, use Firebug or Wireshark to see what is being return by the server for the debug() call—since it will break jQuery's AJAX handler by not being in JSON.

Upvotes: 0

Leo
Leo

Reputation: 6571

Try something like:

$codePassed = $_POST['code']

you might also try putting:

$this->log($codePassed,LOG_DEBUG);

somewhere in there and examine the output in tmp/logs/debug.log

Using firebug will help debug the transport.

Upvotes: 1

Related Questions