I'm currently struggling with integrating Google's reCaptchaV3 into my website, particularly when using the fetch() function. Here is the code snippet I have developed so far:
function sendData(name, email, captcha) {
const info = JSON.stringify({
name: name,
email: email,
captcha: captcha
});
fetch('/verifycontact', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'mode': 'cors'
},
body: info
})
.then(res => {
alert(JSON.stringify(res));
res.json()})
.then(data => {
alert('msg: ' + data.msg + ', score:' + data.score);
})
}
When attempting to print out or log the info object, it works before the first then.(). However, trying to log or alert the res object results in an empty response like this:
{}
Is there something crucial missing from the code? Perhaps related to the headers? I am also noticing that on my server side when I try to console.log(req.body), it seems to be empty as well. The **body object received by my server looks like this:
{ success: false, 'error-codes': [ 'invalid-input-response' ] }