Recaptcha调用中出现

Recaptcha调用中出现

本文介绍了Recaptcha调用中出现"Access-Control-Allow-Origin"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用我的angularJS寄存器形式实现Recaptcha.但是我得到了错误**"XMLHttpRequest无法加载 https://www.google.com/recaptcha/api/verify.所请求的资源上没有"Access-Control-Allow-Origin"标头.

I am implementing recaptcha in my angularJS register form. But I am getting error as**"XMLHttpRequest cannot load https://www.google.com/recaptcha/api/verify. No 'Access-Control-Allow-Origin' header is present on the requested resource.

我已经使用 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>

推荐答案

由于出现您要访问的资源(https://www.google.com/recaptcha/api/verify)不允许来自浏览器的请求(您正在执行的操作),因此出现此错误使用Angular).

You are getting this error because the resource that you are trying to access (https://www.google.com/recaptcha/api/verify) does not allow requests from browsers (which is what you're doing using Angular).

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

Most likely you have to make the http call from another server, but that depends on the API's specs.

有关标题的更多信息:

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

这篇关于Recaptcha调用中出现"Access-Control-Allow-Origin"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 13:06