js刷新验证码:
<a href="javascript:changeImg();" style="color:red; font-size:12px; text-decoration:none;" title="看不清?点击换一张"> <img id="imgcode" src="Handler/CheckCodeHandler.ashx?type=login&stamp=1" width="80" height="30" onclick="changeUrl()"/> </a>
//刷新验证码
function changeImg() { document.getElementById("imgcode").src = "Handler/CheckCodeHandler.ashx?type=login&stamp=" + Math.random(); }
CheckCodeHandler.ashx
using System; using System.Web; namespace YK.WebSite.Handler { /// <summary> /// 用于验证码的处理程序 /// 调用该处理程序时,要加参数type,表明是哪一个功能需要的验证码 /// 获取验证码的SessionKey是YK.Website.CheckCode.[type] /// </summary> public class CheckCodeHandler : IHttpHandler { public HttpResponse Response { get { return HttpContext.Current.Response; } } public HttpRequest Request { get { return HttpContext.Current.Request; } } public void ProcessRequest(HttpContext context) { string type = Request.QueryString["type"]; if (String.IsNullOrWhiteSpace(type)) return; Response.Buffer = true; Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); Response.Expires = 0; Response.CacheControl = "no-cache"; Response.AppendHeader("Pragma", "No-Cache"); string chkCode = String.Empty; byte[] bytes = YK.Common.Util.CheckCodeKit.GetChkCodeBytes(ref chkCode); YK.Core.Context.Context.SetDataBySession("YK.Website.CheckCode." + type, chkCode); Response.ClearContent(); Response.ContentType = "p_w_picpath/Png"; Response.BinaryWrite(bytes); } public bool IsReusable { get { return false; } } } }
把参数的值写成随机数
这样每次点击刷新的时候
缓存就不一样了
就可以起到刷新的效果了