views:

321

answers:

4

Greetings!

I'm having a problem getting a text value of a captcha from a servlet through ajax call.

When my captcha gets created, its text value is written to session, but after refreshing the image itself though ajax call, I only get one old value of the text.

Refreshing the image itself works ok, but I'm stuck getting the correct values from the session on subsequent call.

On page reload I get both the new image and its new text value, no joy with ajax though.

This works great for the image refresh:

$("#asos").attr("src", "/ImageServlet?="+((new Date()).getTime()) )

This call to another method to get text value gives me old stuff:

        $.ajax({
        url:"checkCaptcha",
        type:"GET",
        cache: false,
        success: function( data) {
            alert(data);
        }
    });

Any feedback will be appreciated.

ps: here's the meat of the method getting the call:

        PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    response.setDateHeader("Expires", 0 );

    // Set standard HTTP/1.1 no-cache headers.
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");


    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");

    // Set standard HTTP/1.0 no-cache header.
    response.setHeader("Pragma", "no-cache");

    out.print( request.getSession( ).getAttribute("randomPixValue") );
    out.close();
A: 

Looks like you need to add a file extension to your url. Assuming it's an html page you're making the ajax call to:

$.ajax({
    url:"checkCaptcha.html",
    type:"GET",
    cache: false,
    success: function( data) {
        alert(data);
    }
});
Hooray Im Helping
... it's not mapped to a .html, just "checkCaptcha", works as expected on page reload when the value gets appended into a test div.
vector
@Hooray: he's using JSP/Servlet, not just PHP/HTML or so.
BalusC
Yeah, I realized that after I posted. Sorry for the lame response - proof people shouldn't answer questions while waiting to fall asleep.
Hooray Im Helping
+1  A: 

Let the servlet send the following headers:

response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
Bozho
... tried that before posting as well :-), no joy.
vector
A: 

... progress report:

- session id:

Both methods getting called on the server print the same si FireBug also shows the same si on initial page load and subsequent page reloads They both print the same captcha textual value.

... here's something interesting. When I reload only the image through ajax call, the image gets reloaded, but the method does not print to console.

Method that is supposed to get the text value does print to console the old value

So, image gets reloaded but nothing prints to console and text value get printed but it's old.

When looking at the ajax call in FireBug, what am I looking for exactly? (That's my weakest spot I'm afraid)

... hm ...

vector
A: 

As per the comments on the question, here's a copy of the comment which needs to be reposted as an answer:

Are you using a 3rd party captcha API or a homegrown one? If 3rd party, which one? Are you also sure that they all uses the same session? Debug/print session.getId() in both the image and captcha servlets. Are you sure that the captcha servlet got called? Debug the doGet() method.

BalusC