dchest / captcha

Go package captcha implements generation and verification of image and audio CAPTCHAs.
godoc.org/github.com/dchest/captcha
MIT License
1.92k stars 293 forks source link

Using AJAX #10

Closed jonpchin closed 8 years ago

jonpchin commented 8 years ago

I am trying to implement the captcha with AJAX. Currently when a user submits just digits(runs through the Verify function in captcha.go) the captcha image becomes broken. This occurs regardless if the Reload button is triggered through JavaScript. Implementing strings or strings with digits however allows reload of image but the captcha image breaks again when only digits are entered using AJAX.

I think this is mainly because the entire web page needs to be parsed again and pass in the captcha.New() string through go templates.

So I don't see any way to combine AJAX with this set up as if I have to refresh the entire web page to pass in the captchaId string through golang templates then using AJAX would be pointless. Any ideas to work around this?

dchest commented 8 years ago

Sorry, I don't completely understand the question. How are images become broken?

If you want to generate a new captcha, just create an API which calls captcha.New and replies with captchaId, and call this API from JavaScript, getting new captchaId from server and setting <img src=... to it.

jonpchin commented 8 years ago

My setup is very similar to the one you have in the cap example folder.

After some thinking, I am going to try and call the captcha.New() from the back end when user enters incorrect captcha and then use AJAX to pass the captcha string to the front end to modify the DOM. Not sure if its going to work.