简体   繁体   English

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

[英]codeigniter ajax error user email check

I have created a simple code ajax request for checking email availability in codeigniter framework. 我已经创建了一个简单的代码ajax请求,用于检查codeigniter框架中的电子邮件可用性。 but, I get everytime error. 但是,我每次都会得到错误。 and don't know how to resolve it. 并且不知道如何解决它。 below is my footer js script(after jquery and other external scripts). 下面是我的页脚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);
                    }
                })
            }
        });
    });

and this is my User Controller function to check email in db 这是我的用户控制器功能,用于检查数据库中的电子邮件

    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')));
        }
    }
}

and in the registration form I have this field for email input: 在注册表格中我有这个字段用于电子邮件输入:

<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>

but now every time I change the value of input. 但现在每次我改变输入值。 I get error on console.log 我在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}

anyone knows how to fix it ? 有谁知道如何解决它?

What's standing between you and a successful debug is this part: 您和成功调试之间的关系是这一部分:

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

From the jQuery documentation : jQuery文档

error 错误
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. 类型:Function(jqXHR jqXHR,String textStatus,String errorThrown)请求失败时要调用的函数。 The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. 该函数接收三个参数:jqXHR(在jQuery 1.4.x,XMLHttpRequest中)对象,描述发生的错误类型的字符串和可选的异常对象(如果发生)。 Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". 第二个参数的可能值(除了null)是“timeout”,“error”,“abort”和“parsererror”。 When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." 发生HTTP错误时,errorThrown会收到HTTP状态的文本部分,例如“Not Found”或“Internal Server Error”。 As of jQuery 1.5, the error setting can accept an array of functions. 从jQuery 1.5开始,错误设置可以接受一系列函数。 Each function will be called in turn. 每个函数将依次调用。 Note: This handler is not called for cross-domain script and cross-domain JSONP requests. 注意:不会为跨域脚本和跨域JSONP请求调用此处理程序。 This is an Ajax Event. 这是一个Ajax事件。

What you're logging with console.log is the jqXHR parameter. 您使用console.log记录的是jqXHR参数。 This should work: 这应该工作:

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

Once you get the actual error message you can get to the root of the problem. 一旦得到实际的错误消息,您就可以找到问题的根源。

I have done this exercise before so Here is my Ajax script which i used. 我之前做过这个练习。这是我使用的Ajax脚本。 I have taken a different approach from you to get email availability. 我采取了不同的方法来获取电子邮件。 I haven't put the controller code because it seems slimier to your controller. 我没有放控制器代码,因为它对你的控制器来说看起来更模糊。

$(document).ready(function() {
        //Your Email on key press
       $("#email").keyup(function() {

       var email = $(this).val();
       if (email.length > 3) 
       {    
        $.ajax({
            type: 'POST',
            url: emailAvailability_url,
            data: {
                email: email
            }, success: function(data) {

                // Your Ajax Resonse Here

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

        } 
    });
});

This is the controller code which i used: 这是我使用的控制器代码:

public function check_email_availability(){

    $data = $this->input->post();

    $this->form_validation->set_error_delimiters('<span class="error">', '</div>');     

    if (($this->form_validation->run('email_verification') == FALSE)){          
        echo form_error('email');
    } else {            
        unset($_POST);
        echo '<span style="color:green;" id="emailIsValid">Available</span>';
    }

}

I have added the validation inside the config. 我在配置中添加了验证。

'email_verification' => array(
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'required|max_length[50]|valid_email|is_unique[table_name.email]',
                    'errors' => array(
                            'required' => 'Email  is required.',
                            'valid_email' => 'Plese Enter Valid Email',
                            'is_unique' => 'That Email is already taken. Try Again With New Email.'
                    )
            )
)

intention of answer is just try to help. 答案的意图只是尽力帮助。 So if you want to stick to your method it's totally your choice. 因此,如果您想坚持自己的方法,那么这完全是您的选择。 Thanks 谢谢

I'm so sorry, I have founded solution. 我很抱歉,我已经找到了解决方案。 it is because that I have used an Authentication in the constructor of User controller. 这是因为我在User控制器的构造函数中使用了Authentication。 I have changed form action to another controller and its done. 我已将表单操作更改为另一个控制器并完成了。 now I get the email availability message but if its taken before I got nothing. 现在我收到了电子邮件可用性消息,但是如果它在我什么都没有之前拍摄 no ajax-loading gif shown ! 没有ajax-loading gif显示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM