我愿意site verification在通过用户验证获得 g-recaptcha-response
之后。
我发送带有参数的 xhr POST 并得到 200 OK,但没有应有的响应:
{
"success": true|false,
"error-codes": [...] // optional
}
代码
<script type='text/javascript'>
var onReturnCallback = function(response) {
document.getElementById('resp').innerHTML = response; // works well
//alert('grecaptcha.getResponse() = ' + grecaptcha.getResponse()); // works well too
$.post("https://www.google.com/recaptcha/api/siteverify",
{ secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
response: response,
remoteip : "<?php echo $ip;?>" // optional, does not influence an empty result
}).complete(function( data ) {
alert( "Data returned from POST: " + data.toString() );
console.dir(data);
});
};
</script>
Form.
<form method="post">
<div class="g-recaptcha" data-sitekey="6LdYKQkTAAAAAD9K6-kHspFUPUnftw1RxP5_awi0" data-callback="onReturnCallback" data-theme="light"> </div>
<input name="send" type="submit" />
</form>
我在控制台中打印的对象完全是空的(statusText='error'
除外),请参阅 shot .
控制台还有其他错误:
XMLHttpRequest cannot load https://www.google.com/recaptcha/api/siteverify. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://tarex.ru' is therefore not allowed access.
如何处理?我可以更改原始 header 吗?如何验证?
link到演示。
最佳答案
由于防止 XSS 攻击的所谓“同源策略”(SOP),不可能向网站服务主机以外的主机发出 XHR(“AJAX 请求”)。
但是,您可以从在您自己的主机上运行的 php 代理发布到 reCaptcha 站点。给出了一个例子 in this answer .这也可以防止您的 secret 被查看您的客户端源代码的人公开。
另一种可能性(取决于您要使用的服务)是 JSONP .由于禁止 XHR,但禁止从外部主机加载脚本,因此可以通过查询参数将回调函数的名称添加到脚本 URL。一旦加载了外部资源,就会调用此函数。但据我所知,reCaptcha 不支持 JSONP。
关于javascript - 谷歌新的 reCaptcha 站点验证没有返回任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187476/