1

My buttons click event handler is like this;

$(".imgRefreshContact").click(function () {
       $.ajax({
             url: "/Presentation/Site/Handlers/RefreshCaptcha.ashx",
             type: "POST",
             cache: false,
             async: true,
             success: function () { }
            });
       $("#imgCaptcha").attr('src', '/Presentation/Site/Handlers/CreateCaptcha.ashx');
 });

RefreshCaptcha handler;

public void ProcessRequest(HttpContext context)
    {
        context.Session["CaptchaMetin"] = ConfirmCode.GenerateRandomCode();
    }

CreateCapthca handler;

public void ProcessRequest(HttpContext context)
    {
        ConfirmCode cc = new ConfirmCode(context.Session["CaptchaMetin"].ToString(), 136, 36);

        context.Response.Clear();
        context.Response.ContentType = "image/jpeg";

        cc.Image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        cc.Dispose();
    }

When I clicked an img button, it's working perfect in Chrome and Firefox but fails in IE 9 Debugger enters into RefreshCaptcha handler but not enters into CreateCaptcha handler. So, IE makes ajax request once, not second time.

What is the problem with IE and ajax request

Mehmet Ince
  • 4,059
  • 12
  • 45
  • 65

1 Answers1

2

Use IE developer tools (F12) to debug the request. IE usually caches the request and if you don't change anything it just returns 304 status code (Not modified). To prevent this, add a random query string parameter to your ajax call. Something like:

url: "/Presentation/Site/Handlers/RefreshCaptcha.ashx?rnd=" + Math.Random()
Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • Thank you, it worked great! But I have another question, will IE cache every request by created using Math.Random()? Does this cause performance or memory issue? – Mehmet Ince May 29 '13 at 08:50
  • 1
    If you send a request without changing any parameter, IE returns 304 (not modified). By adding `Math.Random()` every request you send has a new `rnd` query string value. This forces IE to send the request (Because from IE's perspective, this is a different request). I have used this approach in an enterprise solution with no problem. So AFAIK, this does not have any performance/memory issue. – Kamyar May 29 '13 at 12:23