如何为我的reCaptcha ajax api发帖请求? [英] How to do a post request for my reCaptcha ajax api?

查看:128
本文介绍了如何为我的reCaptcha ajax api发帖请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的reCaptcha现在使用ajax实现到我的注册过程..

My reCaptcha is now implemented into my signup process using ajax..

目前无论在验证码文本字段中输入什么,它都不会验证。它只是不断给我输入新词。

Currently no matter what is typed in the captcha text field it never validates. It just keeps giving me new words to type in.

我认为这是因为我需要测试用户是否输入了正确的验证码答案。我需要发送一个POST请求。

I've figured it's because I need to test if the user entered the right captcha answer. I need to send a POST request.

我不知道怎么做...我以前从来没有这样做过,文档不够清晰对我来说..

I have no idea how to do this.. I've never had to do this before and the documentation isn't clear enough for me..

我很感激我能得到的任何帮助。

I would appreciate any help I could get.

这里我的代码:

$(document).ready(function(){

    $('#fhjoinForm').submit(function(e) {

        register();
        e.preventDefault();

    });

});


function register()
{
    hideshow('loading',1);
    error(0);

    $.ajax({
        type: "POST",
        url: "submit.php",
        data: $('#fhjoinForm').serialize(),
        dataType: "json",
        success: function(msg){

                if(parseInt(msg.status)==1)
                {
                Recaptcha.create("pub key xxxxxxxxxxx",
               "recaptcha_div",
                {
                theme: "clean",
                callback: function(){
                // what to do on success
                window.location=msg.txt;
         }

            }

     );
   }

            else if(parseInt(msg.status)==0)
            {
                error(1,msg.txt);
            }

            hideshow('loading',0);
        }
    });

}


function hideshow(el,act)
{
    if(act) $('#'+el).css('visibility','visible');
    else $('#'+el).css('visibility','hidden');
}

function error(act,txt)
{
    hideshow('error',act);
    if(txt) $('#error').html(txt);
}

这是我的帖子请求

<?php
require_once('recaptchalib.php');
$publickey = "pub key hidden"; // you got this from the signup page
$privatekey = "priv key hidden";

$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {
    ?>success<?
}
else 
{
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}
?>


推荐答案

您的jQuery代码看起来像是在创建reCAPTCHA之后表单已提交,因为它在PHP尝试验证后在success()函数中创建它。但实际上,应首先创建并显示reCAPTCHA。

Your jQuery code looks like it's creating the reCAPTCHA after the form is submitted, because it's creating it in the success() function after PHP has tried to validate. But really, the reCAPTCHA should be created and displayed first.

然后在表单提交运行寄存器上,让PHP检查值。

Then on the form submit run register and let PHP check the value.


  1. 显示reCAPTCHA并等待用户提交

  2. 将AJAX发送给PHP并让recaptcha_check_answer完成它的工作

  3. 向用户显示错误或成功。

这篇关于如何为我的reCaptcha ajax api发帖请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆