0

所以今天我试图验证一个验证码,不知何故向我发送了一个错误(我确定我做的很好)。

这是我为验证它而编写的代码(我在前端这样做只是为了使用 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);
      }
    }

我在控制台中打印了 URL,以便我可以手动访问它并检查一切是否正常(没关系)。

这就是我的contact.vue中的内容

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

错误:Uncaught (in promise) SyntaxError: Unexpected end of input at eval即使在 trycatch 中,当我单击错误所在的链接时,它也会指向我所做的行result => result.json()

有什么帮助吗?我正在使用 Nuxtjs

4

1 回答 1

0

}在代码末尾有一个额外的内容。删除它后,我在 VS Code 上没有收到任何其他语法错误。

let url = 'https://www.google.com/recaptcha/api/siteverify?'另外,url = url + 'secret=mysecretkey&response=' + token;为了简洁起见,您可能想要结合起来。

语法错误意味着这是您编写的问题,而不是程序运行方式的问题。

解决这个问题并重新缩进你的代码,我最终得到:

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);
}
于 2021-09-08T01:03:00.010 回答