2016-09-12 3 views
0

내 페이지에서 은 captcha 코드이고 img은 코드를 표시하며, 전체 페이지가 아니라 클릭하면 새로 고침됩니다. chrome에서 코드가 제대로 작동하고 클릭 할 때마다 코드가 새로 고쳐 지지만 에서 img을 클릭하면 captcha 코드를 새로 고치면 아무 것도 나타나지 않고 페이지를 다시로드해야합니다.
IE에서 어떻게이 문제를 해결할 수 있습니까?Ajax를 사용하여 captcha 코드를 새로 고침

<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); 
?> 

답변

1

브라우저는 이미지를 다시 검색하고 캐시에 보이지 않는 것입니다 고유 매개 변수를 사용합니다. 여기 Ajax를 필요가 없습니다

function resetCap(){ 
    document.getElementById('capImg').src = 
          "php/captcha.php?" + new Date().getTime(); 
} 

이 당신이 필요로하는 모두이다.

+0

각 페이지를 다시로드 한 후 첫 번째 클릭을 테스트하고 코드를 변경했지만 IE에서 다음 클릭은 아무 것도하지 않습니다. – Masoud

+0

@Masoud 답변을 업데이트했습니다. Ctrl-F5를 사용하여 IE의 캐시를 지 웠습니까? – user4035

+0

고마워요. 이 답변 및 IE에서 아주 잘 작동하지만 Ajax 사용하지 않고이 수행 할 수 있는지 몰랐습니다. – Masoud

관련 문제