【问题标题】:'Access-Control-Allow-Origin' error in recaptcha callrecaptcha 调用中的“Access-Control-Allow-Origin”错误
【发布时间】:2014-07-15 07:28:58
【问题描述】:

我正在我的 angularJS 注册表单中实现 recaptcha。但我收到错误 **“XMLHttpRequest 无法加载 https://www.google.com/recaptcha/api/verify。请求的资源上不存在“Access-Control-Allow-Origin”标头。

我已使用https://github.com/VividCortex/angular-recaptcha 进行重新验证。

我的代码如下。

<head>
    <script>
            var app = angular.module('testApp', ['vcRecaptcha']);

            app.controller('testCtrl', function ($scope, vcRecaptchaService) {
                console.log("this is your app's controller");

                $scope.model = {
                    key: ' -- public key--'
                };

                $scope.submit = function () {
                    var valid;
                    console.log('Submit button');
                    var challenge =  $('#recaptcha_challenge_field').val();
                    var response =  $('#recaptcha_response_field').val();
                    console.log('challenge' + challenge );
                    $.ajax({
                    url: "https://www.google.com/recaptcha/api/verify",
                    type: 'POST',       
                    data: {
                        privatekey  : '--my private key --',
                        remoteip : '--my ip--',
                        challenge : challenge,
                        response : response,
                    },
                    success: function(data){
                        console.log(" success " + data);
                    },
                    error:function(){
                           console.log(" error occured ");
                    }
                      }); 
                   }
                };
            });
        </script>

    </head>
    <body>
    <div class="container" ng-app="testApp" ng-controller="testCtrl">

        <h1>VividCortex reCaptcha Directive Example</h1>


        <form>
            <div
                vc-recaptcha
                tabindex="3"
                theme="clean"
                key="model.key"
            ></div>

            <!-- Call a method in the scope of your controller to handle data submit -->
            <button class="btn" ng-click="submit()">Submit</button>
        </form>

    </div>
    </body>

【问题讨论】:

  • 还有,你为什么用jquery的ajax?

标签: jquery cors recaptcha


【解决方案1】:

您收到此错误是因为您尝试访问的资源 (https://www.google.com/recaptcha/api/verify) 不允许来自浏览器的请求(这是您使用 Angular 所做的)。

您很可能必须从另一台服务器进行 http 调用,但这取决于 API 的规范。

更多关于标题:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Origin

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 2018-12-12
    • 2017-06-15
    • 2017-06-15
    • 2014-03-05
    • 2013-05-08
    • 1970-01-01
    • 2017-10-24
    • 2021-06-17
    • 2017-12-06
    相关资源
    最近更新 更多