使用 Ajax 刷新验证码
refreshing captcha code using Ajax
在我的页面中,我有一个显示 captcha
代码的 img
和一个在单击时刷新 captcha
代码而非整个页面的 img
。在 chrome
中,它工作得很好,每次单击时都会刷新代码,但在 IE
中,单击 img
时没有任何反应,并且要刷新 captcha
代码重新加载页面。
我如何在 IE
中解决这个问题?
<img name="capImg" id="capImg" src="php/captcha.php">
<img src="images/recaptcha.png" onClick="resetCap();">
脚本:
function resetCap(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
document.getElementById('capImg').src = "php/captcha.php";
}
}
ajaxRequest.open("GET", "php/captcha.php", true);
ajaxRequest.send();
}
captcha.php:
<?php
session_start();
$backImage = imagecreatefrompng('C:\xampp\htdocs\service\images\captchaBack.png');
$shadowColor = imagecolorallocate($backImage, 150, 150, 150);
$textColor = imagecolorallocate($backImage, 50, 50, 50);
$font_path = 'C:\xampp\htdocs\service\fonts\times_new_yorker.ttf';
$text = NULL;
for($i=0; $i<6; $i++) $text .= mt_rand(0,9);
$_SESSION['capCode'] = $text;
imagettftext($backImage, 28, 2, 13, 38, $shadowColor, $font_path, $text);
imagettftext($backImage, 28, 2, 15, 40, $textColor, $font_path, $text);
header('Content-type: image/png');
imagepng($backImage);
imagedestroy($backImage);
?>
使用一个唯一的参数,使浏览器再次检索图像而不查看缓存。这里不需要 Ajax:
function resetCap(){
document.getElementById('capImg').src =
"php/captcha.php?" + new Date().getTime();
}
这就是您所需要的。
在我的页面中,我有一个显示 captcha
代码的 img
和一个在单击时刷新 captcha
代码而非整个页面的 img
。在 chrome
中,它工作得很好,每次单击时都会刷新代码,但在 IE
中,单击 img
时没有任何反应,并且要刷新 captcha
代码重新加载页面。
我如何在 IE
中解决这个问题?
<img name="capImg" id="capImg" src="php/captcha.php">
<img src="images/recaptcha.png" onClick="resetCap();">
脚本:
function resetCap(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
document.getElementById('capImg').src = "php/captcha.php";
}
}
ajaxRequest.open("GET", "php/captcha.php", true);
ajaxRequest.send();
}
captcha.php:
<?php
session_start();
$backImage = imagecreatefrompng('C:\xampp\htdocs\service\images\captchaBack.png');
$shadowColor = imagecolorallocate($backImage, 150, 150, 150);
$textColor = imagecolorallocate($backImage, 50, 50, 50);
$font_path = 'C:\xampp\htdocs\service\fonts\times_new_yorker.ttf';
$text = NULL;
for($i=0; $i<6; $i++) $text .= mt_rand(0,9);
$_SESSION['capCode'] = $text;
imagettftext($backImage, 28, 2, 13, 38, $shadowColor, $font_path, $text);
imagettftext($backImage, 28, 2, 15, 40, $textColor, $font_path, $text);
header('Content-type: image/png');
imagepng($backImage);
imagedestroy($backImage);
?>
使用一个唯一的参数,使浏览器再次检索图像而不查看缓存。这里不需要 Ajax:
function resetCap(){
document.getElementById('capImg').src =
"php/captcha.php?" + new Date().getTime();
}
这就是您所需要的。