0

I have a class in folder 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;
    }

I also have image with property onclick on registration page

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

Can i get data from function GetCaptchaText which returns random number.. with ajax because i always get error when i use this script below?

 function reFreshCaptchaImage() {

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

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

I want to refresh that part without refreshing whole page

Cœur
  • 37,241
  • 25
  • 195
  • 267
None
  • 8,817
  • 26
  • 96
  • 171

1 Answers1

1

You can not call a C# class directly from AJAX. You need to have some kind of handler in between these two.

For example you could setup an ashx file to return an image and in that file you would call your C# class.

Here is a tutorial on using generic handlers (ashx files):
http://www.dotnetperls.com/ashx

and here is a tutorial on creating a captcha through an generic handler:
http://www.codeproject.com/Articles/99148/Simple-CAPTCHA-Create-your-own-in-C

When you have a handler that can return an image, you could simply refresh the image. I found another question here on SO where this subject is discussed:
Refresh image with a new one at the same url

Community
  • 1
  • 1
EmKay
  • 1,089
  • 1
  • 13
  • 28