• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

Having issues using ajax to reload captcha

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am using SimpleCaptcha in my application. The captcha image changes for every new request.

Now i am trying to validate captcha using AJAX and when the validation is successful i will go ahead and submit the form or else i will need to reload the CAPTCHA.

The validation works fine but since the request doesnt change the CAPTCHA also remains the same.

When manual page reloads happens its rendering a CAPTCHA byt AJAX refresh doesnt render a new one.

Is there a way to create a new request in JAVA or AJAX everytime i validate or is there any other solution??

Posts: 1210
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an img element to display the captcha, then the key to refreshing its image is to change its src attribute to something different.
If src is not changed, a browser would display the cached version of that URL, and you'll see the same image every time which is not what you want.
But if src is changed, a browser would then asynchronously and automatically make a new request to that changed URL, and display whatever image bytes it receives based on the response MIME type.

One simple way to change src attribute without changing the servlet behind that URL is to add current time as a query parameter on each request.
Your client side javascript should be something like:

The http://your_app_url/generatecaptcha.do is the URL for SimpleCaptchaServlet (or your custom captcha generating servlet) which responds with image bytes with "image/png" MIME type. The t query parameter can be ignored on server side - its purpose is just to vary the URL.
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic