点击图片,图片src重新加载(用于图片验证码)

效果图:

后台给的链接直接插入html中img的src里,点击图片重新加载这个src因为src的内容一直不变,比如都是

src="http://www.xxx.com/xxx/get?tockenId=QWERTYUISDFGHJEW"

如果刷新页面这个链接返回的图片会改变,为了做到点击图片只重载这个src而整个页面不刷新,采用的方法是

document.getElementById("img").src="http://www.xxx.com/xxx/get?tockenId=QWERTYUISDFGHJEW?"+Math.random();

在后面加上Math.random()

完整代码如下:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>测试页</title>
      <style>
         #img{cursor:pointer;}
      </style>
   </head>
<body>
<img onclick="changeCode();" src="http://www.xxx.com/xxx/get?tockenId=QWERTYUISDFGHJEW" id="img"/>
<script>
   function changeCode(){
      document.getElementById("img").src="http://www.wubapai.com/verifycode/get?tockenId=QWERTYUISDFGHJEW?"+Math.random();
   }
</script>
</body>
</html>

 

 

 

 

 

 

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
在 Spring Boot 中实现图片验证码可以通过以下步骤进行: 1. 添加依赖:在 `pom.xml` 文件中添加以下依赖项: ```xml <dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency> ``` 2. 创建验证码生成器:创建一个验证码生成器类,用于生成验证码图片。可以参考以下示例代码: ```java import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class CaptchaConfig { @Bean public DefaultKaptcha defaultKaptcha() { DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", "no"); properties.setProperty("kaptcha.textproducer.font.color", "black"); properties.setProperty("kaptcha.image.width", "150"); properties.setProperty("kaptcha.image.height", "50"); properties.setProperty("kaptcha.textproducer.char.length", "4"); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } } ``` 3. 创建验证码接口:创建一个接口或控制器,用于处理验证码相关的请求,并将生成的验证码图片返回给前端。示例代码如下: ```java import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; @RestController public class CaptchaController { private final DefaultKaptcha captchaGenerator; @Autowired public CaptchaController(DefaultKaptcha captchaGenerator) { this.captchaGenerator = captchaGenerator; } @GetMapping("/captcha") public void getCaptcha(HttpServletResponse response) throws IOException { // 生成验证码 String captchaText = captchaGenerator.createText(); BufferedImage captchaImage = captchaGenerator.createImage(captchaText); // 将验证码文本存储在 session 中(可根据具体需求存储在其他地方) // request.getSession().setAttribute("captcha", captchaText); // 将验证码图片输出给前端 response.setContentType("image/jpeg"); try (OutputStream outputStream = response.getOutputStream()) { ImageIO.write(captchaImage, "jpeg", outputStream); outputStream.flush(); } } } ``` 4. 前端页面:在前端页面中使用 `<img>` 标签来显示验证码图片,并添加一个刷新按钮。示例代码如下: ```html <!DOCTYPE html> <html> <head> <title>Captcha Demo</title> </head> <body> <h1>Captcha Demo</h1> <img id="captchaImage" src="" alt="Captcha"> <button onclick="refreshCaptcha()">Refresh</button> <script> function refreshCaptcha() { var captchaImage = document.getElementById("captchaImage"); captchaImage.src = "/captcha?t=" + new Date().getTime(); } // 页面加载刷新验证码 window.onload = function() { refreshCaptcha(); }; </script> </body> </html> ``` 这样就可以在 Spring Boot 中实现图片验证码了。用户可以通过访问 `/captcha` 路径获取验证码图片,并在前端页面中显示。每次刷新验证码时,可以调用 `refreshCaptcha()` 函数来更新验证码图片。请根据自己的需求进行相应的调整和扩展。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辣姐什么鬼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值