user14623448
user14623448

Reputation:

ReCAPTCHA verification is sending a SyntaxError

So today I was trying to verify a CAPTCHA, and somehow is sending me an error (I'm sure I'm doing things okay).

This is the code I wrote to verify it (I'm doing this on the frontend just for education purposes with the CAPTCHA)

try {
        const token = document.querySelector('#g-recaptcha-response').value;
        let url = 'https://www.google.com/recaptcha/api/siteverify?secret=mysecretkey&response=token'
        fetch( url, {
          method: 'POST',
          mode: 'no-cors',
        })
        .then(response => response.json())
        .then(data => console.log(data));
      } catch (err) {
        console.log(err);
      }
    }

I printed in the console the URL so I can access it manually and check if everything is okay (It is okay).

And this is what I have in my contact.vue

<div class="g-recaptcha" data-sitekey="6LfC0kwcAAAAAMZZA0swdErB5_h8y6R_H7hZ85E7" data-size="normal"></div>

The error: Uncaught (in promise) SyntaxError: Unexpected end of input at eval even inside a trycatch, and when I click on the link where the error is, it points to the line where I do result => result.json()

Any help with this? I'm using Nuxtjs

Upvotes: 0

Views: 304

Answers (1)

2br-2b
2br-2b

Reputation: 466

You've got an extra } at the end of the code. After removing that, I'm not getting any other syntax errors on VS Code.

Plus, you may want to combine let url = 'https://www.google.com/recaptcha/api/siteverify?' and url = url + 'secret=mysecretkey&response=' + token; for brevity's sake.

Syntax errors mean it's a problem with what you wrote, not with how the program runs.

Fixing this and re-indenting your code, I end up with:

try {
    const token = document.querySelector('#g-recaptcha-response').value;
    let url = 'https://www.google.com/recaptcha/api/siteverify?secret=mysecretkey&response=' + token;
    console.log(url);
    fetch( url, {
        method: 'POST',
        mode: 'no-cors',
    })
    .then(response => response.json())
    .then(data => console.log(data));
} catch (err) {
    console.log(err);
}

Upvotes: 0

Related Questions