如何使用ajax/jquery访问类?

如何使用ajax/jquery访问类?,jquery,ajax,Jquery,Ajax,我在文件夹ImageCaptcha中有一个类 private string GetCaptchaText(int length) { var possibleCaptchaCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var result = ""; Random random = new Random(); for (int index = 0; index &

我在文件夹ImageCaptcha中有一个类

 private string GetCaptchaText(int length)
    {
        var possibleCaptchaCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var result = "";
        Random random = new Random();
        for (int index = 0; index < length; index++)
        {
            result += possibleCaptchaCharacters[(int)Math.Ceiling(random.NextDouble() * possibleCaptchaCharacters.Length - 1)].ToString();

        }
        return result;
    }
我希望在不刷新整个页面的情况下刷新该部分

您不能直接从AJAX调用C#类。在这两者之间需要某种处理程序

例如,您可以设置一个ashx文件来返回图像,并在该文件中调用您的C#类

以下是关于使用通用处理程序(ashx文件)的教程:

下面是一个关于通过通用处理程序创建验证码的教程:

当您有一个可以返回图像的处理程序时,您可以简单地刷新图像。我在这里发现了另一个关于这个主题的问题:

<button id="refresh"><img src="~/Images/captcha-refresh.png" height="22" width="29" alt="" onclick="reFreshCaptchaImage()"></button>
 function reFreshCaptchaImage() {

        $.ajax({
            type: 'GET',
            url: "../CaptchaImage/test/Index",
            success: function (msg) {

                alert(msg);
            },
            error: function () {
                alert("error");
            }
        });
    }