我在尝试在节点中使用reCAPTCHA时收到错误:“无法读取未定义的属性'g-recaptcha-response'”

时间:2019-11-09 17:22:38

标签: javascript node.js

提交带有reCAPTCHA元素的表单时,出现此错误:无法读取未定义的属性'g-recaptcha-response'

这是我接收reCAPTCHA的代码

app.post('/captcha', function(req, res) {
  console.log("BRUH")
  if(req.body['g-recaptcha-response'] === undefined || req.body['g-recaptcha-response'] === '' || req.body['g-recaptcha-response'] === null)
  {
    return res.json({"responseError" : "Please select captcha first"});
  }
  const secretKey = "6Lez4cEUAAAAAHxMs9ChAG9CfU4Piu23ql2PYV2t";

  const verificationURL = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + req.body['g-recaptcha-response'] + "&remoteip=" + req.connection.remoteAddress;

  request(verificationURL,function(error,response,body) {
    body = JSON.parse(body);

    if(body.success !== undefined && !body.success) {
      return res.json({"responseError" : "Failed captcha verification"});
      console.log("error");
    }
    res.json({"responseSuccess" : "Sucess"});
    console.log("succes");
  });
});

0 个答案:

没有答案