codeigniter ajax错误用户电子邮件检查

Ali*_*ani 10 javascript php ajax jquery codeigniter

我已经创建了一个简单的代码ajax请求,用于检查codeigniter框架中的电子邮件可用性.但是,我每次都会得到错误.并且不知道如何解决它.下面是我的页脚js脚本(在jquery和其他外部脚本之后).

<script>

    $(document).ready(function() {
        $("#loading").hide();
        $("#email").blur(function() {
            var email_val = $("#email").val();
            var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
            if (filter.test(email_val)) {
                // show loader
                $('#loading').show();
                jQuery.ajax({
                    type: "POST",
                    url: "<?php echo site_url()?>users/check_email_ajax",
                    dataType: 'json',
                    data: {
                        '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>',
                        'email': 'email_val'
            },

                success: function (response) {
                    if(response){
                        $('#loading').hide();
                        console.log(response.message);
                        $('#msg').html(response.message)
                        $('#msg').show().delay(10000).fadeOut();
                    }
                },
                    error: function(error){
                        $('#loading').hide();
                        console.log("There is some errors on server : " + error.error);
                    }
                })
            }
        });
    });
Run Code Online (Sandbox Code Playgroud)

这是我的用户控制器功能,用于检查数据库中的电子邮件

    public function check_email_ajax(){
    // allow only Ajax request
    if($this->input->is_ajax_request()) {
        // grab the email value from the post variable.
        $email = $this->input->post('email');
        // check in database - table name : users  , Field name in the table : email
        if(!$this->form_validation->is_unique($email, 'users.email')) {
            // set the json object as output
            $this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'The email is already taken, choose another one')));
        }else{
            $this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'you can use this email')));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在注册表格中我有这个字段用于电子邮件输入:

<div class="col-sm-5">
                <input type="email" id="email" name="email" class="form-control">
                <span id="loading"><img src="<?php echo base_url(); ?>ajax-loader.gif" alt="Ajax Indicator" /></span>
            <div id="msg"></div>
            </div>
Run Code Online (Sandbox Code Playgroud)

但现在每次我改变输入值.我在console.log上收到错误

There is some errors on server : function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决它?

小智 5

您和成功的调试之间存在的部分是:

error: function (error) {
    $('#loading').hide();
    console.log("There is some errors on server : " + error.error);
}
Run Code Online (Sandbox Code Playgroud)

jQuery文档中

error
类型:Function(jqXHR jqXHR,字符串textStatus,字符串errorThrown)如果请求失败,则要调用的函数。该函数接收三个参数:jqXHR对象(在jQuery 1.4.x中为XMLHttpRequest),一个描述发生错误的类型的字符串,以及一个可选的异常对象(如果发生)。第二个参数(除null外)的可能值为“超时”,“错误”,“中止”和“ parsererror”。发生HTTP错误时,errorThrown会接收HTTP状态的文本部分,例如“未找到”或“内部服务器错误”。从jQuery 1.5开始,错误设置可以接受函数数组。每个函数将依次调用。注意:对于跨域脚本和跨域JSONP请求,不会调用此处理程序。这是一个Ajax事件。

您要记录的console.log是jqXHR参数。这应该工作:

error: function (jqXHR, errorType, error) {
    $('#loading').hide();
    console.log(errorType + ": " + error);
}
Run Code Online (Sandbox Code Playgroud)

一旦获得实际的错误消息,您就可以找到问题的根源。


Ali*_*ani 3

非常抱歉,我已经找到了解决方案。这是因为我在用户控制器的构造函数中使用了身份验证。我已将表单操作更改为另一个控制器并完成。现在我收到了电子邮件可用性消息,但如果在我什么也没得到之前就被采取了。没有显示ajax加载gif!