点击刷新验证码

这里只是贴前端部分代码

 

页面

 <p style="margin-bottom: 0px" id="codeBox">
                    <img src="${rc.contextPath}/gif/getGifCode" id="codeImg" alt="验证码"  width="146px" height="33px">
                    <a href="javascript:void(0);" id="changeCode">看不清,换一张</a>
                </p>

 

js部分,没错这个是不需要ajax刷新的。

后面的参数必须

①:带时间参数

//刷新验证码
$(document).on('click','#changeCode',function(){
    var img=document.getElementById("codeImg");
    img.src=path+"/gif/getGifCode?"+new Date().getTime(); 
});

②:带随机数

//刷新验证码
$(document).on('click','#changeCode',function(){
  var randomnum = Math.random(); var img=document.getElementById("codeImg"); img.src=path+"/gif/
getGifCode?"+randomnum;
});

 后台

/**
     * 获取验证码(Gif版本)
     * @param response
     */
    @RequestMapping(value={"/getGifCode","/changeGifCode"},method= RequestMethod.GET)
    public void getGifCode(HttpServletResponse response, HttpServletRequest request){
        try {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/gif");
            /**
             * gif格式动画验证码
             * 宽,高,位数(验证码位数)。
             */
            Captcha captcha = new GifCaptcha(290,33,6);
            //输出
            captcha.out(response.getOutputStream());
            HttpSession session = request.getSession(true);
            String id = request.getSession().getId();
            //存入redis
            redisService.setString("code"+id,captcha.text().toLowerCase());
            //System.out.println("code"+id+"*****"+captcha.text().toLowerCase());
        } catch (Exception e) {
            System.err.println("获取验证码异常:"+e.getMessage());
        }
    }

 

实现效果

 

 

转载于:https://www.cnblogs.com/zeussbook/p/10785381.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值