yii2.0 点击验证码图片不刷新

时间:2025-03-20 23:05:49

修改vender\yiisoft\yii2\captcha\CaptchaAction下的run方法

public function run()
{
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
// AJAX request for regenerating code
$code = $this->getVerifyCode(true);
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'hash1' => $this->generateValidationHash($code),
'hash2' => $this->generateValidationHash(strtolower($code)),
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
'url' => Url::to([$this->id, 'v' => uniqid()]),
];
} else {
$this->setHttpHeaders();
Yii::$app->response->format = Response::FORMAT_RAW;
return $this->renderImage($this->getVerifyCode());
}
} 将
return $this->renderImage($this->getVerifyCode());这行代码getVerifyCode()函数加一个参数true改为:
getVerifyCode(true)