dtq7387 2014-12-24 03:36
浏览 145
已采纳

在codeigniter上使用ajax验证recaptcha的invalid-request-cookie

thanks for reading my question, i have some issues trying to validate a recaptcha, with codeigniter with jquery ajax method, i always get invalid-requste-cookie, here is part of my code

Part of "welcome" controller

public function anunciese() 
    {
        $data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();
        $data['main_content'] = 'welcome/anunciese';
        $this->load->view('includes/'.$this->config->config["tema"].'/template' , $data);
    }

    public function validar_recaptcha()
    {
        $this->recaptcha->recaptcha_check_answer(
                    $_SERVER['REMOTE_ADDR'],
                    $this->input->post('recaptcha_challenge_field'),
                    $this->input->post('recaptcha_response_field'));
        if ($this->recaptcha->getIsValid() == false) 
        {
            $datos['success'] = false;
            $datos['titulo'] = "ERROR";
            $datos['mensaje'] = $error = $this->recaptcha->getError(); 
        } 
        else 
        {
            $datos['success'] = true;
            $datos['titulo'] = "";
            $datos['mensaje'] = "";
        }
        echo json_encode($datos);
    }

Here is the view (part of It)

<form class="form-horizontal" method="post">
  <fieldset>
    <div class="form-group">
      <label for="inputEmail" class="col-lg-2 control-label">Email</label>
      <div class="col-lg-10">
        <input class="form-control" id="inputEmail" placeholder="Email" type="text">
      </div>
    </div>
    <div class="form-group">
      <label for="inputNombre" class="col-lg-2 control-label">Nombre</label>
      <div class="col-lg-10">
        <input class="form-control" id="inputNombre" placeholder="Nombre" type="text">
      </div>
    </div>

    <div class="form-group">
        <?php echo $recaptcha_html; ?>
    </div>

    <div class="form-group">
        <input type="button" class="btn btn-primary" value="Suscribirme Gratis" onClick="SalvarProspecto()" />
    </div>

And finally my js code

function ValidarRecaptcha()
    {
        var phpencode = true;
        var urlx = base_url + 'welcome/validar_recaptcha';
        $.ajax({
        type: "POST",
        url: urlx,
        data: {},
        async: false,
        success: function (data) {
            if (phpencode == true) {
                data = $.parseJSON(data);   
            }
            console.log(data) //Solo para propositos de debug
            if (data.success) {
                return true;
            } else {
                return false;
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Failed " + urlx);
            alert(xhr.responseText);
            alert(thrownError);
        }
    });
    }

I am using this library https://github.com/Cnordbo/RECaptcha-for-Codeigniter

Any help would be appreciated

  • 写回答

1条回答 默认 最新

  • douyiqi9640 2015-01-02 20:36
    关注

    Just made it without ajax requests, and making it a basic form, with the form helper, and the codeigniter documentation here, like that example https://github.com/Cnordbo/RECaptcha-for-Codeigniter

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 为什么zynq CAN IP 无法进入config配置模式,XCan_SelfTest函数失效?
  • ¥15 Pycharm中程序直接运行可以但进入调试报错
  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥15 我的显卡支持CUDA最高版本是12.3,这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8,需要下载旧版本的VS2022吗
  • ¥100 采用栈和深度优先算法取消graphics库采用其他方式显示路径打印显示路径取消graphics库
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)