使用AJAX的g-recaptcha-response

使用AJAX的g-recaptcha-response,ajax,validation,recaptcha,Ajax,Validation,Recaptcha,我有一个联系人表单,它通过ajax请求将信息发送到php脚本。现在,我喜欢实现谷歌的“我不是机器人”重述 问题是,我不知道如何使用ajax传输g-recaptcha-response。 如何验证它并将结果发送到send_contactrequest_process.php 森登 Vielen Dank für Deine Nachricht ReCaptcha验证与表单提交不同 有关如何嵌入reCaptcha和验证用户和站点的信息,请参见 验证成功后,您可以提交表格。以自动提交的方式查看。

我有一个联系人表单,它通过ajax请求将信息发送到php脚本。现在,我喜欢实现谷歌的“我不是机器人”重述

问题是,我不知道如何使用ajax传输g-recaptcha-response。 如何验证它并将结果发送到send_contactrequest_process.php


森登

Vielen Dank für Deine Nachricht


ReCaptcha验证与表单提交不同

  • 有关如何嵌入reCaptcha和验证用户和站点的信息,请参见
  • 验证成功后,您可以提交表格。以自动提交的方式查看。 但如果您更喜欢ajax表单提交,您可能会在siteverify ajax调用后嵌套提交,如下所示:

    var onReturnCallback = function(response) { 
    var url='proxy.php?url=' + 'https://www.google.com/recaptcha/api/siteverify';
    $.ajax({ 'url' : url,  // site verification ajax request
       dataType: 'json',
       data: { response: response},
       success: function( data  ) {  
            var res = data.success.toString();  
            if (res)
            { 
               var firstname = $('#contact-form-firstname').val(); 
               ....
               $.ajax({ //AJAX request of form submission
                    type: "POST",
                    url: "../include/process/send_contactrequest_process.php",
                    data: {firstname: firstname...},
                    success: function () {
                        $('.kontakt-form-success-message').css( "display","inline" );
                    },
                });
    
             } 
         }  // end success 
      });  // end $.ajax
    
    };  // end onReturnCallback
    

  • 只需调用这个函数
    grecaptcha.getResponse()
    像这样:

    if($("#kontakt-form").valid()){
    
        var firstname = $('#contact-form-firstname').val(); //Validierung der Form-Daten
        var lastname = $('#contact-form-lastname').val();
        var email = $('#contact-form-email').val();
        var telephone = $('#contact-form-telephone').val();
        var message = $('#contact-form-message').val();
    
        $.ajax({ //AJAX request
            type: "POST",
            url: "../../../include/process/send_contactrequest_process.php",
            data: {
              firstname: firstname,
              lastname: lastname,
              email:email,
              telephone:telephone,
              message:message,
              recaptcha:grecaptcha.getResponse()
            },
            success: function () {
                $('.kontakt-form-success-message').css( "display","inline" );
            },
        });
    }