문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top