调用图像处理程序时,Ajax请求无效

时间:2013-04-18 13:01:42

标签: jquery asp.net ajax generic-handler

我的按钮点击事件处理程序是这样的;

$(".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处理程序;

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

CreateCapthca处理程序;

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();
    }

当我点击一个img按钮时,它在Chrome和Firefox中运行良好,但在IE 9中失败了 调试器进入RefreshCaptcha处理程序但不进入CreateCaptcha处理程序。 所以,IE一次发出ajax请求,而不是第二次。

IE和ajax请求有什么问题

1 个答案:

答案 0 :(得分:2)

使用IE开发人员工具(F12)调试请求。 IE通常会缓存请求,如果您不更改任何内容,则只返回304状态代码(未修改)。要防止这种情况,请在ajax调用中添加随机查询字符串参数。类似的东西:

url: "/Presentation/Site/Handlers/RefreshCaptcha.ashx?rnd=" + Math.Random()