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 () {
            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?


Try something like:

$codePassed = $_POST['code']

you might also try putting:


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

Using firebug will help debug the transport.

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.

Need Your Help

how to auto export files when merge a branch?


I'm working with a website using a git repository with 2 branches master-branch and dev-branch.

Problem in URL validation using ASP.Net Regular Expression Validator

asp.net regex validation

I'm trying to use the ASP.Net Regular Expression Validator to validate a URL field. URL is www.tachibana.co.jp/tokyosys.htm. Validation expression used is ValidationExpression="http(s)?://([\w-]+\....