I'm tryin to validate an input field with an ajax call to a cakephp controller My Ajax is:
我正在尝试使用对cakephp控制器的ajax调用来验证输入字段我的Ajax是:
$("#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?
我很确定我在这里使用$ this-> params错误来访问从ajax请求发送的数据。我应该做什么呢?
2 个解决方案
#1
1
Try something like:
尝试以下方法:
$codePassed = $_POST['code']
$ 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
在那里的某个地方检查tmp / logs / debug.log中的输出
Using firebug will help debug the transport.
使用firebug将有助于调试传输。
#2
0
Don't know why it would be returning null, but I normally use $this->data
to fetch form data.
不知道为什么它会返回null,但我通常使用$ this-> 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.
你试过调试($ this-> params)吗?如果您没有非AJAX表单来测试请求,请使用Firebug或Wireshark查看服务器返回调试()调用的内容 - 因为它将通过不在JSON中来破坏jQuery的AJAX处理程序。
#1
1
Try something like:
尝试以下方法:
$codePassed = $_POST['code']
$ 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
在那里的某个地方检查tmp / logs / debug.log中的输出
Using firebug will help debug the transport.
使用firebug将有助于调试传输。
#2
0
Don't know why it would be returning null, but I normally use $this->data
to fetch form data.
不知道为什么它会返回null,但我通常使用$ this-> 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.
你试过调试($ this-> params)吗?如果您没有非AJAX表单来测试请求,请使用Firebug或Wireshark查看服务器返回调试()调用的内容 - 因为它将通过不在JSON中来破坏jQuery的AJAX处理程序。