gpt4 book ai didi

javascript - 使用 jQuery ajax 的隐形 ReCaptcha

转载 作者:数据小太阳 更新时间:2023-10-29 03:49:43 25 4
gpt4 key购买 nike

我正在尝试使用 jQuery 和“ajax”请求在表单中实现最新的 ReCaptcha(又名“隐形”ReCaptcha)。
ReCaptcha 文档:https://developers.google.com/recaptcha/docs/invisible

我的表单:

<form id="myForm" >
<input type="email" name="email" /><br />
<input type="password" name="password" /><br/>
<!--<input type="submit" value="log in" />-->
<button class="g-recaptcha" data-sitekey="6LdK..." data-callback="onSubmit">log in</button>
</form>
<div id="status"></div>

我的 javascript (jQuery):

<script>

function onSubmit(token){
document.getElementById("myForm").submit();
}

$(document).ready(function(){

$("#myForm").submit(function(event){
event.preventDefault();
var datas = $("#myForm").serialize();
$.ajax({
type: "POST",
url: "test.php",
data: datas,
dataType: "json",
beforeSend: function(){
$("#status").html("logging in...");
},
success: function(response){
$("#status").html(response.text);
if(response.type=="success"){
window.location.replace("/myaccount");
}
},
error: function(){
$("#status").html("Failed.");
}
});
});

});
</script>

ReCaptcha 需要设置一个“数据回调”,我不确定如何绑定(bind)我已经存在的“.submit(function(event)”函数。
我的“onSubmit()”技巧没有奏效,它忽略了“ajax”并刷新了页面。

如何在我的“datas”变量中发送“g-recaptcha-response”值以将其发布到 test.php?

最佳答案

所以这就是我在 Invisible reCAPTCHA 的文档中进一步挖掘并学习了一些 jQuery 之后解决它的方法,因为我对 JS 不是很熟悉(很酷的东西):

我的 head 标签带有 javascript(和一些 CSS 来移除丑陋的 Google 徽章):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit&hl=fr" async defer></script>

<style>
.grecaptcha-badge{
display:none;
}
</style>

<script>
var onloadCallback = function(){
grecaptcha.render("emplacementRecaptcha",{
"sitekey": "YOUR_RECAPTCHA_SITEKEY_HERE",
"badge": "inline",
"type": "image",
"size": "invisible",
"callback": onSubmit
});
};
var onSubmit = function(token){
var userEmail = $("#userEmail").val();
var userPassword = $("#userPassword").val();
var userTfaOtp = $("#userTfaOtp").val();
$.ajax({
type: "POST",
url: location.href,
data:{
userEmail: userEmail,
userPassword: userPassword,
userTfaOtp: userTfaOtp,
userJetonRecaptcha: token
},
dataType: "json",
beforeSend: function(){
$("#statutConnexion").html("Traitement de votre requête d'authentification en cours...");
},
success: function(response){
$("#statutConnexion").html(response.Message);
if(response.Victoire){
$("#formulaireConnexion").slideUp();
window.location.replace("/compte");
}
else{
grecaptcha.reset();
}
},
error: function(){
$("#statutConnexion").html("La communication avec le système d'authentification n'a pas pu être établie. Veuillez réessayer.");
grecaptcha.reset();
}
});
};
function validate(event){
event.preventDefault();
$("#statutConnexion").html("Validation de votre épreuve CAPTCHA en cours...");
grecaptcha.execute();
}
function onload(){
var element = document.getElementById("boutonConnexion");
element.onclick = validate;
}
</script>

HTML:

<div id="formulaireConnexion">
<input type="email" name="userEmail" id="userEmail" placeholder="Courriel" title="Courriel" required="required" /><br />
<input type="password" name="userPassword" id="userPassword" placeholder="Mot de passe" title="Mot de passe" required="required" /><br/>
<input type="text" name="userTfaOtp" id="userTfaOtp" placeholder="Double authentification (optionnelle)" autocomplete="off" pattern="[0-9]{6}" title="Six caractères numériques" maxlength="6" /><br />
<div id="emplacementRecaptcha"></div>
<button id="boutonConnexion">Connexion</button>
</div>
<div id="statutConnexion"></div>
<script>onload();</script>

如果您还需要整个 PHP,请告诉我,因为它超出了这个问题的范围。您可能需要更改上面 JS 中的“url: location.href”,因为在我的例子中,渲染 HTML 表单和 JS 以及处理 POST 变量的脚本是相同的(不是很好,测试目的)。基本上我只是验证 POST 变量,然后最终返回一个 json,如:

$jsonVictoire = true; // boolean
$jsonMessage = 'anything you want to tell your visitor'; // string

$return =
json_encode(
array(
'Victoire'=>$jsonVictoire,
'Message'=>$jsonMessage
)
);
die($return);

关于javascript - 使用 jQuery ajax 的隐形 ReCaptcha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43837623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com