Webmaster
Webmaster

Reputation: 69

JavaScript error: Invalid Token when using ReCaptcha API

I have recently applied on my website an invisible Google ReCaptcha, but it is showing Unexpected Error: Invalid token= when I use the following code:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
    function onSubmit = function(token) {
       document.getElementById("admin-login").submit();
    }
</script>

I have a form button as follows:

<button data-sitekey="My Site Key" data-callback='onSubmit' class="btn btn-default submit g-recaptcha" name="login">Log in</button>

Any help is appreciated, thank you.

Upvotes: 2

Views: 3360

Answers (2)

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

function onSubmit = function(token) { is incorrect.

It needs to be

var onSubmit = function(token) {

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
    var onSubmit = function(token) {
    document.getElementById("admin-login").submit();
    }
</script>

JavaScript allows different ways to create a function. It would be better if you read through following in detail.

1. Function Declaration

function onSubmit(){ }

2. Function Expression

var onSubmit = function(){ }

3. Arrow Functions
([params]) => { //function Body }

4. Generator Functions (More Advanced)
function* generatorFunction(){ yield 5; }

Upvotes: 5

pistou
pistou

Reputation: 2867

Well, there is two ways to create/declare a function:

function onSubmit(token) {
    document.getElementById("admin-login").submit();
}

or

var onSubmit = function(token) {
    document.getElementById("admin-login").submit();
};

You kinda mixed the two.

Upvotes: 1

Related Questions