【问题标题】:google invisible reCaptcha server side validation is failing谷歌隐形 reCaptcha 服务器端验证失败
【发布时间】:2018-04-28 07:42:34
【问题描述】:

我已在我的应用程序中集成了不可见的 reCaptcha,客户端响应将作为解决图像挑战的一部分。然后我调用一个角度函数来使用下面的代码验证服务器端的用户响应。其中 onloginSubmit(token) 是成功回调。

<button id="btnLogin" ng-disabled="loginForm.$invalid" 
                                class="g-recaptcha primarybtn margin-left-zero form-control-input"
                                data-sitekey="{{public_key}}"
                                data-callback='onloginSubmit'>{{'label_login' |
                                translate}}</button>

<script>
   function onloginSubmit(token) {
       angular.element(document.getElementById('loginForm')).scope().verifyReCaptcha(token);
   };
 </script>

在角度上,我正在调用 verifyReCaptcha,如下所示。

$scope.public_key = "------ My Site Key -------";
  $scope.private_key = "------ My Secret Key -------";
  $scope.verifyReCaptchaURL = "https://www.google.com/recaptcha/api/siteverify";

$scope.verifyReCaptcha = function(reCaptchaToken){
       var captchaData = {
              secret : $scope.private_key,
              response : reCaptchaToken
      }

      $http({
          headers: {
            'Accept': 'application/json,text/plain',
            'Content-Type': 'application/json;application/x-www-form-urlencoded;charset=utf-8;',
          },
          url: $scope.verifyReCaptchaURL,
          method: 'POST',
          data: captchaData
        }).success(function (data) {
             console.log("success");
             $scope.login();
        }).error(function (data) {
             console.log("error");
             $window.location.reload(true);
        });
  };

当我点击 api 服务 https://www.google.com/recaptcha/api/siteverify 时。我收到以下错误。

Failed to load https://www.google.com/recaptcha/api/siteverify: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.

我找不到有关该问题的更多文档。

我做错了什么,如果有任何错误,recaptcha 不会出现并且我使用的登录按钮无法响应。

n 请求我提到方法为 Post,该方法被覆盖为选项,并且我发送的请求有效负载不存在。这是我在网络选项卡中得到的

Request URL:https://www.google.com/recaptcha/api/siteverify Request Method:OPTIONS Status Code:405 Remote Address:10.120.118.50:8080 Referrer Policy:no-referrer-when-downgrade

【问题讨论】:

    标签: javascript angularjs cors recaptcha invisible-recaptcha


    【解决方案1】:

    大部分事情你都做得很好。一件事是要求在您的应用程序中与外部域通信,以便您可以包含 HTTP 标头内容类型,包括 JSONP 格式。

     $http({
          headers: {
            'Accept': 'application/json,text/plain',
            'Content-Type': 'application/jsonp;application/x-www-form-urlencoded;charset=utf-8;',
          },
          url: $scope.verifyReCaptchaURL,
          method: 'POST',
          data: captchaData
        }).success(function (data) {
             console.log("success");
             $scope.login();
        }).error(function (data) {
             console.log("error");
             $window.location.reload(true);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2013-12-10
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      相关资源
      最近更新 更多