我是通过ajax来提交验证码,可是总是返回false, 为了查找原因,我分别在官方blog demo和我自己的一个项目里部署了一模一样的代码,可是在blog demo里却完全正常工作,在我的项目里却总是返回false,困惑了我两天,先看看我自己项目的代码,代码本身应该没有问题:
先是model:
<?php
| class DCaptcha extends CFormModel
| {
| public $verifyCode;
|
|
| /**
| * @return array validation rules for model attributes.
| */
| public function rules()
| {
| return array(
| array(‘verifyCode’, ‘captcha’,‘allowEmpty’=> !extension_loaded(‘gd’))
| );
| }
|
| /**
| * @return array customized attribute labels (name=>label)
| */
| public function attributeLabels()
| {
| return array(
| ‘verifyCode’=>‘Verification Code’,
| );
| }
|
| }
| ?>
然后是controller:
<?php
|
| class DaogouController extends CController
| {
|
| public function actions() {
| return array(
| ‘captcha’=> array(
| ‘class’ => ‘CCaptchaAction’,
| ‘backColor’ => 0xffffff,
| ‘testLimit’=>999,
| ),
| );
| }
public function actionCheckCaptcha()
| {
| $_SESSION = array();
| var_dump($_SESSION);
| $data = $_GET[‘ccode’];
| $result = array(‘state’=>false,‘msg’=>’’);
| $captcha = new DCaptcha();
| $captcha->verifyCode = $data;
| $r = $captcha->validate();
| var_dump($_SESSION);
| var_dump($r);
| var_dump($captcha);
| return $r;
| }
| }
最后是videw:
<input type="text" class="input_field name" id="register_code"/>
<div class=“captcha” ><?php $this->Widget(“CCaptcha”,array(‘buttonLabel’=>‘换一张’)); ?></div>
和ajax请求:
$(’#register_code’).blur(function(){
var obj = $(this);
var val = obj.attr("value");
$.getJSON("/index.php/[color="#FF0000"]daogou[/color]/checkCaptcha?ccode="+val+"&random="+Math.random(),function(data){});
});
再看看在blog demo里的ajax请求:
$.getJSON("/index.php?r=[color="#FF0000"]site[/color]/checkcaptcha&ccode="+val+"&random="+Math.random(),function(data){});
注意ajax请求中controller的区别,下面是对我的项目操作的输出结果:
正确输入code:
964
打印的结果:
array(0) {
}
array(4) { [color="#8B0000"]//正确的code被保存到了site.captcha下,而保存在daogou.captcha下的code不知道从哪里来的,所以,不管这么样总是返回false,why?[/color]
["Yii.CCaptchaAction.dfd593a.[color="#FF0000"]site[/color].captcha"]=>
string(7) "zovacar"
["Yii.CCaptchaAction.dfd593a.site.captchacount"]=>
int(1)
["Yii.CCaptchaAction.dfd593a.[color="#FF0000"]daogou[/color].captcha"]=>
string(7) "zobaywt"
["Yii.CCaptchaAction.dfd593a.daogou.captchacount"]=>
int(26)
}
bool(false)
object(DCaptcha)#13 (6) {
["verifyCode"]=>
string(7) "zovacar"
["_errors":"CModel":private]=>
array(1) {
["verifyCode"]=>
array(1) {
[0]=>
string(35) "The verification code is incorrect."
}
}
["_validators":"CModel":private]=>
object(CList)#14 (5) {
["_d":"CList":private]=>
array(1) {
[0]=>
object(CCaptchaValidator)#15 (10) {
["caseSensitive"]=>
bool(false)
["captchaAction"]=>
string(7) "captcha"
["allowEmpty"]=>
bool(false)
["attributes"]=>
array(1) {
[0]=>
string(10) "verifyCode"
}
["message"]=>
NULL
["skipOnError"]=>
bool(false)
["on"]=>
array(0) {
}
["safe"]=>
bool(true)
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
}
["_c":"CList":private]=>
int(1)
["_r":"CList":private]=>
bool(false)
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
["_scenario":"CModel":private]=>
string(0) ""
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
谁能告诉我是什么原因?我该怎么做才行呢? 我用的是v1.1.4