9

I have a google recaptcha but my form is ajaxed so I need to get the 'input value' of the token generated by the captcha, I inspect the page to find the token and I did fin the token

<input type="hidden" id="recaptcha-token" value="03AHJ_VuucWtkVREJrdNs4CGxDBOVJ0NF5mr94-pKbmRE46-VjgtdPrnS3pPtub-fAuqGZHTwoZgbeFGrbe6gMeMuDTtsu1AmHXPkwdlO2n_zRwmnueSBkwDfzr1JLXjX50qF20yWDjV7S74za9SgYTWaNiwYZmljmFQ2niJt7fqR0CncIQtHuTtkrJszZqJDKyCfFGKpKtGEzYOCd6xGOM54QD9C4bhujbswyuCbOpXKMBoBdEtSthCsOllxIZPgATXdqfhAD5D-rgUb6wvvS0KIJJaYyQ8pzZHTNI6y1Mv20LY5dfkKGUaCR6e9F4WnuU8Fd8ZIRXRVrvZdg2U3XUfkJsojUQmYbvCtkjzZ_a49SwKEtU8X8jYVtTk_C5TvxQqEH8NbM1P5yJm-Ua5b4jVaOUp9df0QiZbVH2PlQOIXtPiVk21y_Ff2YaqTpxe2hgmLjdSSfhP3bKQ9L82zB-wRopATkcVOuoGWyx9k8L8zpQ5ZudQtSobFsf3UYg3NhtuBZeeuDkHefyEWk4_Ji-oIp4N2qh9Wv4UKZllSJjwsebtNY_mI7QCon0mKy5ppiJ8vbZU8Q9DM8RQyKsGI8OA3hN8WgD3jijA">

so no I need to capture the value and send it with the request but for some reason it does not store the value in the variable

here is the jquery code

 $(document).on('click', '#IDcontactSubmitBtn', function(event) {
    event.preventDefault();
    var recaptchaToken = $('#recaptcha-token').val();
    console.log(recaptchaToken);
});

even though I did can see the token when I inspect the element it is not stored in the variable, I have no Idea why, may be its not even the right way to implement google recaptcha? pls help, thank you.

2
  • your code looks correct. Try changing $(document) to $('body') Commented May 18, 2016 at 9:42
  • tried, It doesn't work
    – Mikail
    Commented May 18, 2016 at 10:36

3 Answers 3

9

In order to capture the token you should use this code

var token = $("#g-recaptcha-response").val();
2
  • After running into this issue this was the solution that helped me. Even though when you look at the code the token seems to be stored in a different id, this id will get you the token. Commented Dec 5, 2016 at 1:25
  • 2
    This doesn't look reliable if Google tries to change it's internal implementation a bit.
    – nice_dev
    Commented Jan 26, 2019 at 12:25
3

Do as the docs suggest you to do.

var token = grecaptcha.getResponse();
1

I found its working.

<!DOCTYPE html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Place Holder</title>
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

        <input type="hidden" id="recaptcha-token" value="03AHJ_VuucWtkVREJrdNs4CGxDBOVJ0NF5mr94-pKbmRE46-VjgtdPrnS3pPtub-fAuqGZHTwoZgbeFGrbe6gMeMuDTtsu1AmHXPkwdlO2n_zRwmnueSBkwDfzr1JLXjX50qF20yWDjV7S74za9SgYTWaNiwYZmljmFQ2niJt7fqR0CncIQtHuTtkrJszZqJDKyCfFGKpKtGEzYOCd6xGOM54QD9C4bhujbswyuCbOpXKMBoBdEtSthCsOllxIZPgATXdqfhAD5D-rgUb6wvvS0KIJJaYyQ8pzZHTNI6y1Mv20LY5dfkKGUaCR6e9F4WnuU8Fd8ZIRXRVrvZdg2U3XUfkJsojUQmYbvCtkjzZ_a49SwKEtU8X8jYVtTk_C5TvxQqEH8NbM1P5yJm-Ua5b4jVaOUp9df0QiZbVH2PlQOIXtPiVk21y_Ff2YaqTpxe2hgmLjdSSfhP3bKQ9L82zB-wRopATkcVOuoGWyx9k8L8zpQ5ZudQtSobFsf3UYg3NhtuBZeeuDkHefyEWk4_Ji-oIp4N2qh9Wv4UKZllSJjwsebtNY_mI7QCon0mKy5ppiJ8vbZU8Q9DM8RQyKsGI8OA3hN8WgD3jijA">


        <button type="button" id="IDcontactSubmitBtn" >Submit</button>
        <script type="text/javascript">        

        $(document).on('click', '#IDcontactSubmitBtn', function(event) {
            event.preventDefault();
            var recaptchaToken = $('#recaptcha-token').val();
            console.log(recaptchaToken);
        });

    </script>

    </body>
</html>

let me know if your code is live, it might help us a lot to find the problem.

7
  • its on my localhost, maybe the value is not getting stored because its inside an iframe, I'll post the entire page here in my question
    – Mikail
    Commented May 18, 2016 at 10:29
  • whoa..... if its an iframe then that is for sure that you will not able to fetch it. Commented May 18, 2016 at 10:30
  • is there a solution for that? I can't find any info on implementing google captcha with ajax :(
    – Mikail
    Commented May 18, 2016 at 10:34
  • wait i am finding and let you know. . . Commented May 18, 2016 at 10:36
  • 1
    thank you for help, I just find the solution I should have use this as a selector $("#g-recaptcha-response").val()
    – Mikail
    Commented May 18, 2016 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.