我在使用fetch()时遇到问题,无法获取数据

问题描述 投票:1回答:1

我正在尝试在我的网站上实现Googles reCaptchaV3,并且在使用fetch()函数时遇到问题。这就是我到目前为止得到的代码:

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);
    }) 
}

我尝试对info对象发出警报或console.log(),当我尝试console.log()]时,它会在第一个then。()之前执行console.log()。 >或警告res对象,它只是像下面这样返回空:

{}

代码中是否缺少某些内容?也许标题?在我的服务器上,我正在尝试console.log(req.body)

,但我得到的还是一样,它是empty。我的** body对象随此到达我的服务器:
{ success: false, 'error-codes': [ 'invalid-input-response' ] }
    

我正在尝试在我的网站上实现Google的reCaptchaV3,并且在使用fetch()函数时遇到问题。所以这是我到目前为止得到的代码:function sendData(name,email,captcha){...

javascript json fetch recaptcha captcha
1个回答
0
投票

我发现了问题。我需要使用bodyParser

© www.soinside.com 2019 - 2024. All rights reserved.