我已经在我的应用程序中集成了不可见的reCaptcha,客户端响应是解决图像挑战的一部分。 然后,我调用一个 Angular 函数来使用以下代码在服务器端验证用户响应。 其中 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>
在角度中,我调用验证重新验证码如下。
$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不会出现,我使用的登录按钮无法响应。
在我提到的请求中,该方法作为 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
大部分事情你都做得很好。一件事是要求在您的应用程序中与外部域通信,以便您可以包含 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);
});