折雨的天空
go的验证码类
2018-11-29 我好笨


刚刚开始了解go,肯定是使用开源类,类地址:



github.com/mojocn/base64Captcha

这里主要说一下遇到的问题,用的beego,建了一个地址/login/code用于生成验证码,代码如下:







func (c *LoginController) Code() {
//数字验证码配置
//字符,公式,验证码配置
var configC = base64Captcha.ConfigCharacter{
Height: 45,
Width: 125,
//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
Mode: base64Captcha.CaptchaModeNumber,
ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
IsShowHollowLine: false,
IsShowNoiseDot: false,
IsShowNoiseText: false,
IsShowSlimeLine: false,
IsShowSineLine: false,
CaptchaLen: 5,
}
//创建数字验证码.
//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
_, capD := base64Captcha.GenerateCaptcha("", configC)
//以base64编码
base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
c.Ctx.WriteString(base64stringD)
}



在html标签中,写上img标签,地址指向上面的控制器地址,控制器地址正常访问,但是图像无法显示



后来把图片改成了异步请求才行,记录一下:



<img src="/login/code" id="code_img" />
<a id="kanbuq" href="javascript:;">看不清,换一张</a>

--------------------------------------------------------------------------------------------------------
<script>
$(document).ready(function () {
getcode();
})
function getcode() {
$.get('/login/code',function (data) {
$("#code_img").prop('src',data);
})
}
$("#kanbuq").on('click',function () {
getcode();
})
</script>



发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容