验证码的问题:没有“访问控制允许来源”标头的请求的资源present甚至总是显示我在angularJS添加页眉 [英] reCaptcha issues : No 'Access-Control-Allow-Origin' header is present on the requested resource always shows even I have add header in angularJS
问题描述
我在谷歌的reCAPTCHA的一些问题
验证码是罚款,这是正常显示,但是当我提交,我有连接问题,当我发送POST请求
i have some issues in google reCaptcha The captcha is fine, it's show normally, but when I submit it, I have connection issues when I send a POST request to
https://www.google.com/recaptcha/api/verify
在这里的错误日志:
XMLHttpRequest cannot load https://www.google.com/recaptcha/api/verify. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3002' is therefore not allowed access. The response had HTTP status code 405.
在这里我的html code:
here my html code:
<div class="form-group" style="margin-bottom: 20px;">
<div class="text-center center-block">
<div class="text-center center-block" vc-recaptcha
tabindex="3"
theme="white"
key="model.key"
lang="en"
</div>
<button class="btn" ng-click="submit()">Submit</button>
</div>
</div>
这是我controller.js
And this is my controller.js
$scope.model = {
key: //THIS IS MY PUBLIC KEY
};
$scope.submit = function() {
var valid;
var ip;
$http.jsonp('http://ipinfo.io/?callback=JSON_CALLBACK')
.success(function(data) {
console.log("stateChangeStart ip = " + data.ip);
ip = data.ip;
}).then(function() {
$http({
method: 'POST',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST'
},
url: 'https://www.google.com/recaptcha/api/verify',
data: {
'privatekey': /* THIS IS MY PRIVATE KEY */,
'remoteip': ip,
'challenge': vcRecaptchaService.data().challenge,
'response': vcRecaptchaService.data().response
}
}).success(function(result, status) {
console.log('it work');
}).error(function(data, status, headers, config) {
console.log('error');
});
});
};
我添加页眉存在,但它总是说
I have add headers there, but it always says
没有'访问控制允许来源标头present的要求
资源
No 'Access-Control-Allow-Origin' header is present on the requested resource
有人可以帮我吗?
谢谢
can somebody help me? thank you
我使用铬,我已经启用了我的Chrome这个插件CORS:
I am using chrome and I have enabled CORS in my Chrome with this addon :
<一个href=\"https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related\"相对=nofollow>在Chrome浏览器启用CORS
和现在给我的另一个错误:
and now it give me another error:
XMLHttpRequest cannot load https://www.google.com/recaptcha/api/verify. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:3002' is therefore not allowed access.
和更改我的controller.js这个:
and i change my controller.js into this:
$scope.model = {
key: //THIS IS MY PUBLIC KEY
};
$scope.submit = function() {
var valid;
var ip;
$http.jsonp('http://ipinfo.io/?callback=JSON_CALLBACK')
.success(function(data) {
console.log("stateChangeStart ip = " + data.ip);
ip = data.ip;
}).then(function() {
$http({
method: 'POST',
headers: {
'Access-Control-Allow-Origin': 'http://localhost:3002',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS'
},
url: 'https://www.google.com/recaptcha/api/verify',
data: {
'privatekey': /* THIS IS MY PRIVATE KEY */,
'remoteip': ip,
'challenge': vcRecaptchaService.data().challenge,
'response': vcRecaptchaService.data().response
}
}).success(function(result, status) {
console.log('it work');
}).error(function(data, status, headers, config) {
console.log('error');
});
});
};
但错误总是显示,请帮助我。
but that error is always shows, please help me.
推荐答案
在我来说,我使用reCapchaLib这个错误修正:的 https://github.com/google/recaptcha/blob/1.0.0/php/recaptchalib.php
In my case I use reCapchaLib and this error fixed: https://github.com/google/recaptcha/blob/1.0.0/php/recaptchalib.php
和使用code PHP:
And use this code PHP:
<?php
error_reporting(E_ALL ^ E_NOTICE); //variables not declared warning off
// tu clave secreta
$secret = "xxxxxx"; //get from https://www.google.com/recaptcha/
// tu site key
$sitekey = "xxxxxxx"; //get from https://www.google.com/recaptcha/
require_once "recaptchalib.php";
$response = null;
$reCaptcha = new ReCaptcha($secret);
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
if ($_POST["g-recaptcha-response"]) {
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
echo "<h2>Valores del formulario</h2>";
if (isset($_POST["nombre"])){
$nombre = $_POST["nombre"];
echo "<p>nombre: $nombre</p>";
}
if ($response != null && $response->success) {
echo "<font color='blue'>Captcha Ok!</font>";
}
else
echo "<font color='red'>Error en Captcha!</font>";
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="language" content="es">
<title>Test Capcha</title>
<!-- reCAPTCHA Script -->
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body >
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Test reCAPTCHA con formulario Post y PHP</h2>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" novalidate method="POST" action="?">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Nombre *" id="nombre" name="nombre" required data-validation-required-message="Ingrese nombre">
<p class="help-block text-danger"></p>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<center><div class="g-recaptcha" data-sitekey="<?php echo $sitekey; ?>"></div><br></center>
<button type="submit" class="btn btn-xl">Enviar Mensaje</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
</body>
</html>
这篇关于验证码的问题:没有“访问控制允许来源”标头的请求的资源present甚至总是显示我在angularJS添加页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!