使用Ajax刷新验证码

时间:2016-09-12 09:06:40

标签: php ajax captcha

在我的网页中,我有一个显示System.Text.StringBuilder csv = new System.Text.StringBuilder(); String separator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator; List<String> listProperties = new List<string>(); csv.AppendLine("sep=" + separator); foreach (System.Windows.Controls.GridViewColumn col in myGridView.Columns) { // Check if column is displayed if (col.ActualWidth > 0) { // Get Header value string header = col.Header as String; //Check if Header is not empty if (!String.IsNullOrWhiteSpace(header)) { // Write in firte line csv.Append(header + separator); // Get if columns is binding a property System.Windows.Data.Binding binding = col.DisplayMemberBinding as System.Windows.Data.Binding; if (binding != null) { // Get the name of property listProperties.Add(binding.Path.Path); } } } } // Write first line csv.AppendLine(); foreach (myItem item in myListView.Items) { foreach (String property in listProperties) { // Get and write value for property object value = GetPropValue(item, property); csv.Append(value + separator); } csv.AppendLine(); } System.IO.File.WriteAllText(path, csv.ToString()); public static object GetPropValue(object src, string propName) { return src.GetType().GetProperty(propName).GetValue(src, null); } 代码的img和一个captcha代码,用于在点击时刷新img代码,而不是整页。在captcha中,它可以很好地运行并在每次点击时刷新代码,但在chrome点击IE时没有任何反应,并且用于刷新img代码重新加载页面。
如何在captcha

中解决此问题
IE

脚本:

<img name="capImg" id="capImg" src="php/captcha.php">
<img src="images/recaptcha.png" onClick="resetCap();">

captcha.php:

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();
}

1 个答案:

答案 0 :(得分:1)

使用一个唯一的参数,使浏览器再次检索图像,而不是查看缓存。你不需要Ajax:

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

这就是你所需要的一切。