验证码图像自定义在mvc中不更新

本文关键字:更新 mvc 图像 自定义 验证 | 更新日期: 2023-09-27 18:09:55

一切都很好,除了图像不刷新,而使用jquery改变源代码,同时尝试新的验证码,总是需要刷新页面,然后图像更新。

CSHTML:

<p>
 <img id="captchaimage" src="/User/ShowCaptchaImage" />
 <a href="javascript:;" class="f-captcha" title="refresh captcha"><span class="icon-f-reset"></span><span>Try New Captcha</span></a>
</p>

我使用自定义Captcha类继承ActionResult。和ExecuteResult为:

c#:

        public override void ExecuteResult(ControllerContext context)
        {
               // Create image code here , i have implemented.
               //and add response as:
                HttpResponseBase response = context.HttpContext.Response;
                response.ContentType = "image/GF";
                bmp.Save(response.OutputStream, ImageFormat.Gif);
                bmp.Dispose();
                g.Dispose();
         }
JQUERY:

$(document.body).on("click", ".f-captcha", function () {
     $('#captchaimage').attr('src', '/User/ShowCaptchaImage');
});

但是当我点击尝试新的captcha锚标记时,我需要刷新页面。

验证码图像自定义在mvc中不更新

尝试使用唯一的字符串与img源

$(document.body).on("click", ".f-captcha", function () {
      var randLetter =Math.floor(Math.random() * 5854553);
     $('#captchaimage').attr('src', '/User/ShowCaptchaImage?ver='+randLetter);
});

嗨,我的结构有同样的问题,但我在网上找到解决方案,试试这些,这些将为我工作。这可能是因为浏览器从catch内存中获取图像

          $(document).ready(function () {
               $(".f-captcha").click(function () {                  
                   d = new Date();
                   $("#captchaimage").attr("src", "/User/ShowCaptchaImage?" + d.getTime());
               });
           });