简体   繁体   中英

Captcha refresh using jQuery problem

I tried researching on Google and I browsed through some question here. I tried some of the solutions but I can't get them to work.

<td>
    <img id="captcha" src="captcha.php" name="captcha" />
    <img src="refresh.jpg" width="25" alt="" id="refresh_captcha" name="refresh_captcha" />
    <input type="text" name="txtCaptcha" id="txtCaptcha" size="10" />
</td>

The code below doesn't work:

$('.refresh_captcha').click(function(){
    $('img').attr('src', 'captcha.php?cache=' + new Date().getTime());
});

I also tried this:

$("#refresh_captcha").click(function() {
    $("#captcha").attr("src","captcha.php?r=" + Math.random());
});

And this as well:

jQuery(function($) {
    jQuery('#captcha').after("<a href=\"#\" id=\"refresh_captcha\">Refresh<\/a>");
    jQuery('body').delegate('#refresh_captcha','click',function(){
        jQuery.ajax({
            'success':function(html){
                jQuery("#captcha").attr("src",html)
            },
            'url':'/captcha.php?refresh=1',
            'cache':false
        });
        return false;
    });
});

Anyone can help me?

        function fcapCha() {
            var timestamp = (new Date()).getTime();
            var newSrc = $(".capChaCaptchaImgID").attr("src").split("?");
            newSrc = newSrc[0] + "?" + timestamp;
            $(".capChaCaptchaImgID").attr("src", newSrc);
            $(".capChaCaptchaImgID").slideDown("fast");
        }
        function resetCapcha() {
            fcapCha();
            $('.acapChapinputCaptcha').val('');
        }

best regard!

Did you try to empty the cache as well? (EDIT: yes, you did...) Did the problem appear in all browser? I reckon that your browser caches the old image, because your direct call seems to work. Try cache: false instead of 'cache' = false

For me this solution using ajax-calls and Java Spring in backend worked:

function generateCaptcha() {

    $.ajaxSetup({
      cache: false
    });

    var timestamp = (new Date()).getTime();

    requestMappingCaptcha = "/generateCaptcha";

    jQuery.get(requestMappingCaptcha, timestamp, function(data) {
                $("#captchaImg").slideUp("fast");

                if (!$.browser.msie || ($.browser.msie && $.browser.version == "9.0")) { 
                        // animate reloadArrows
                        $("#reloadArrows").rotate({
                        angle:0, 
                        animateTo:360
                    });  
                }

                // setting new source
                var newSrc = $("#captchaImg").attr("src").split("?");
                newSrc = newSrc[0] + "?" + timestamp;
                $("#captchaImg").attr("src", newSrc);
                $("#captchaImg").slideDown("fast");
        });
}

And the HTML for that:

                        <div class="container captcha">
                            <spring:url value="/captcha.jpeg" var="url"/>
                            <div class="step2_captcha">
                                <img id="captchaImg" src="${url}" alt="Generatin captcha..." width="180" height="42" border="0" name="captchaImg"/>
                            </div>
                            <span class="help captcha">
                                <img src="/r.png" onclick="generateCaptcha();" id="reloadArrows" title="Reload Captcha"/>
                            </span>
                            &nbsp;
                        </div>

What does it do?? By clicking the icon the function generateCaptcha will be called. The request mapping called a server function (in my case java) and renders a new captcha to the same URL the old one was. Its importent to disable caching and send a timestamp with the get . After doing that a little jQuery magic is done, the source of the captcha gonna changed to the image-Url + timestamp.

Hope it'll work for you php as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM