重新加载Captcha不适用于jquery PHP

I am using jquery php captcha in magento.Captcha is getting generated fine but I also want to refresh it but not getting how to do it.Here is my code which I used.

HTML Code

    <span id="reload_captcha_span"><img id="captcha_image" src="<?php echo $this->getUrl('helloworld/index/captcha'); ?>"/></span>
<span id="reload_captcha">reload</span>

JQuery Code

jQuery('#reload_captcha').click(function(){
                jQuery.ajax({
                        url: '<?php echo $this->getUrl('helloworld/index/captcha'); ?>',
                        success:function(data){
                            jQuery('#reload_captcha_span').html("");
                            jQuery('#reload_captcha_span').html(data);
                        }
                    }); 
            });

PHP Code

session_start();
        $ranStr = md5(microtime());
        $ranStr = substr($ranStr, 0, 6);
        $_SESSION['cap_code'] = $ranStr;
        $newImage = imagecreatefromjpeg("cap_bg.jpg");
        $txtColor = imagecolorallocate($newImage, 0, 0, 0);
        imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
        header("Content-type: image/jpeg");
        imagejpeg($newImage);

In response I am getting unknown data may be that is image itself.

Please help me.

Thanks in advance!

1个回答

Try this:

jQuery('#reload_captcha').click(function(){
    var d = new Date();
    jQuery('#captcha_image').attr('src', '<?=$this->getUrl('helloworld/index/captcha')?>?'+d.getTime());
});

To fetch the same image url, but we add a random (time) parameter, so it's not fetched from the cache.

douxi4114
douxi4114 浏览器(几乎)总是在调用相同的URL时使用缓存的资源(取决于头文件)。 添加GET参数以避免这种情况是一种古老的技术;)
6 年多之前 回复
dongnuo2879
dongnuo2879 是的,它有效。 非常感谢。 请告诉我你是怎么知道它是缓存的问题。除了增加时间之外我也尝试过相同的问题
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐